1

I have a array called $urls, I want to delete members with value null. I want first submit every member to a function that checks if a member is null, and then I delete the member from array. E.X.

$urls=array();
$url[0]=$_POST['urla'];
$url[1]=$_POST['urlb'];
$url[2]=$_POST['urlc'];
$url[3]=$_POST['urld'];
$url[4]=$_POST['urle'];

Well, I want to delete $urls members that has no value (because user didn't fill out the fields ), How can I do it? thank you for your help

4
  • 3
    hint: array_filter Commented Jun 22, 2016 at 7:40
  • 1
    Read php.net/manual/en/function.array-filter.php Commented Jun 22, 2016 at 7:40
  • 1
    Filtering seems unnecessary here, if you just need the values reindexed you can also do $url = array_values($_POST);. Note that null implies that values might not exist, so your current code will generate warnings for every non-existing element. Also note that you can use arrays in html which would probably make all this redundant. Commented Jun 22, 2016 at 7:44
  • Thank you all for your amazing help :) Commented Jun 22, 2016 at 8:25

4 Answers 4

2

Simply use array_filter(), which conveniently handles this for you:

<?php
print_r(array_filter($urls));
?>
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it in below mentioned ways:-

1.array_filter():-

<?php
 array_filter($_POST);
 $urls = $_POST;
 print_r($urls);
?>

2.Use array_values():-

<?php
   $urls = array_values($_POST);
   print_r($urls);
?>

Reference:-

http://php.net/manual/en/function.array-filter.php

http://php.net/manual/en/function.array-values.php

Comments

1
<?php  
$urls = array_filter($url);
?>

Comments

0

Please check the request post members like following

if(isset($_POST['urla']) && $_POST['urla'] != '') 
{
    $url[0]=$_POST['urla'];
}

if not working please send me more inputs.

1 Comment

Question is about using custom function to check if array item is empty. Where is it in your answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.