Jump to content

Checking if multidimensional array is empty?


gatoruss

Recommended Posts

I have a for with multiple test input fields.  Specifically the user submits multiple names and ages.  The form is submitted using the POST method.  Here is a code snippet:

 


<form action = {$_SERVER['PHP_SELF']} method = 'post' >

for($i=1; $i<=10; $i++){

     <input type = 'text' name = 'name[]' size ='25' maxlength = '50'  />
     <input  type = 'text' name = 'age[]' size ='25' maxlength = '50'  />

}

<input type="submit" />

</form>

 

I want to check if the user clicks submits without entering values in the text fields.

 

I have tried this:

 

if(empty($_POST['name']) && empty($_POST['age'])){

     echo "All the entries were blank, please enter name(s) and age(s) on the left and click submit.  Thank you.";

}

 

However, this doesn't parse as "true" when I click submit with the fields empty.

 

Is it not possible to  use "empty()" with arrays and/or multidimensional arrays?

Link to comment
Share on other sites

quick google search led me to http://php.net/manual/en/function.empty.php where someone made a function i think would work for you

<?php
function array_empty($mixed) {
    if (is_array($mixed)) {
        foreach ($mixed as $value) {
            if (!array_empty($value)) {
                return false;
            }
        }
    }
    elseif (!empty($mixed)) {
        return false;
    }
    return true;
}
?>

Link to comment
Share on other sites

quick google search led me to http://php.net/manual/en/function.empty.php where someone made a function i think would work for you

<?php
function array_empty($mixed) {
    if (is_array($mixed)) {
        foreach ($mixed as $value) {
            if (!array_empty($value)) {
                return false;
            }
        }
    }
    elseif (!empty($mixed)) {
        return false;
    }
    return true;
}
?>

 

Yes, I saw that.  The purpose of my question was to understand why empty($_POST['name']) did not work with a multidimensional array.  I guess to better understand how empty() worked?

 

Note - I have been searching (with Google) for an answer to my question, but have not been able to find something that helped explain how/if you could use empty() with multidimensional array.

 

Thanks.

 

Russ

Link to comment
Share on other sites

Empty did not work on that array because technically the array is not empty. Doing a print_r on the array for the code you posted yielded this:

 

Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => ) 

 

As you can see there are indexes in that array, which means it is not empty. So empty works as expected, you just did not realize that the indexes were being created just not populated.

 

The code I used to generate the above item:

 

<?php
if (isset($_POST['submit'])) {
print_r($_POST['name']);
}
?>
<form action = <?php echo $_SERVER['PHP_SELF']; ?> method = 'post' >
<?php
for($i=1; $i<=10; $i++){
?>
     <input type = 'text' name = 'name[]' size ='25' maxlength = '50'  />
     <input  type = 'text' name = 'age[]' size ='25' maxlength = '50'  />
<?php
}
?>
<input type="submit" name="submit" value="submit" />
   
</form>

 

If you would prefer to see for yourself :)

Link to comment
Share on other sites

Yes, I saw that.  The purpose of my question was to understand why empty($_POST['name']) did not work with a multidimensional array.  I guess to better understand how empty() worked?

 

First let me start by saying that $_POST['name'] is not a multidimensional array (though $_POST in your case is).  As already mentioned above, the value of $_POST['name'] when no input boxes have been filled in is an array of empty strings: in plain PHP code it would look like $_POST['name'] = array('', '', '', '', '', '', '', '', '', ''); — an array containing 10 items which are all empty strings.

 

Now, lets look at empty. It will return TRUE if an "empty array" is provided. An empty array is one which contains no items — array()  Can you see why yours is different to an empty array?

 

As for some form of solution to your problem. There are generally multiple ways to achieve the same result in PHP and here's one:

 

$names = array_filter($_POST['name'], 'strlen');
if (empty($names)) {
    // No names were submitted
}

 

This loops over all of the values in $_POST['name'] and if the value has any string length (ie. there is something there) it will get added on to the $names array. Conversely, to help clarify, if any of the items in $_POST['name'] have no string length (they weren't filled in in the form) then they won't get added to the $names array.  Then we can simply check if the $names array has any values to see if there were any names submitted.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.