tags:

views:

41

answers:

1

I have an array that may have some null or blank values in it. Is there a PHP function that I can use to traverse this array to simply find out if there is a value anywhere?

For example:

[0]=>
[1]=>
[2]=> test

I'd like to test against the total number of values present, if any. count() won't work because this is only a portion of this array and it always returns 1 which is not accurate.

Array
(
    [inputbox] => Array
        (
            [name] => Array
                (
                    [0] => New Text Document.txt  <------- This is what I need to test
                    [1] => 
                )

            [type] => Array
                (
                    [0] => text/plain
                )

            [tmp_name] => Array
                (
                    [0] => /var/tmp/phpLg2rFl
                    [1] => 
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 
                )

            [size] => Array
                (
                    [0] => 0
                    [1] => 
                )
        )
)
+1  A: 

I don't understand your question very well, but perhaps you're looking for array_filter()?

array_filter($arr) will return an array with all empty values removed, so in your case only the index 2 with the value of test will be preserved, you could use count() afterwards.


In light of your comment:

if (count(array_filter($arr)) > 0)
{
    echo '$arr has values';
}

Beware that if you don't provide the second argument for array_filter() all values that can be converted to false will be dropped, such as 0's. If you want to remove only empty values you can do:

if (count(array_filter($arr, 'isset')) > 0)
{
    echo '$arr has values';
}

Or (my preferred version):

if (count(array_filter($arr, 'strlen')) > 0)
{
    echo '$arr has values';
}

You may also be interested in a Coalesce function for PHP.


In light of your last comment I still think array_filter() works, (assuming $_FILES) try this:

if (count(array_filter($_FILES['inputbox']['name'], 'strlen')) > 0)
{
    echo count($_FILES['inputbox']['name']) . ' files';
    echo '<br />';
    echo count(array_filter($_FILES['inputbox']['name'], 'strlen')) . ' files set';
}
Alix Axel
Thanks Alix. It may work as I've not used array_filter before. I'm just trying to find out if there is a value present in the array is all. There can be multiple textboxes that builds this array and if the user started at box #5 instead of number 1, I have to be able to test that.
Timay
@Timay: Updated my answer. `array_filter()` should work for you.
Alix Axel
Thanks a bunch Axil.. I tried this and had a look at the docs but don't get the correct results back. Even if I enter a value in say, textbox #9 the count always returns 0.
Timay
Hmm.. I may not be able to use array_filter. I just noticed that this is a multi-dimensional array. I will post the structure above.
Timay
@Timay: Check my answer again.
Alix Axel
Axil. Thanks. You were correct, that works. Please explain the strlen function. I don't understand how this works when you are not getting the results of strlen. Could you please explain?
Timay
@Timay: Sure, the `array_filter()` function calls `strlen()` for each one of the array values, as the result is converted to boolean it will be `false` when `strlen() == 0` and `true` when `strlen() >= 1`.
Alix Axel
ahhhh... So actually, strlen is the real magic here in that all of the null or blank values in the array return false so they are NOT included in the output. Sweet... :)
Timay
Thanks again for all the help Alix, I really appreciate it.
Timay
@Timay: No problemo! =)
Alix Axel