3

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] => 
                )
        )
)
Timay
  • 135
  • 1
  • 1
  • 6
  • 2
    Is it a flat array? Or can it be a multidimensional array as well? If the former: there's no need for recursion. – Decent Dabbler Jan 30 '10 at 08:40
  • When you say total number present, do you mean the total number of non-null values or total number of items in the array or total number of items that you traversed? – Anthony Jan 30 '10 at 08:42
  • 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 for that so that I can display the proper message. – Timay Jan 30 '10 at 08:44

1 Answers1

2

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';
}
Community
  • 1
  • 1
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • 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 Jan 30 '10 at 08:40
  • @Timay: Updated my answer. `array_filter()` should work for you. – Alix Axel Jan 30 '10 at 08:43
  • 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 Jan 30 '10 at 08:49
  • 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 Jan 30 '10 at 08:50
  • @Timay: Check my answer again. – Alix Axel Jan 30 '10 at 08:59
  • 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 Jan 30 '10 at 09:07
  • @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 Jan 30 '10 at 09:13
  • 1
    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 Jan 30 '10 at 09:16
  • Thanks again for all the help Alix, I really appreciate it. – Timay Jan 30 '10 at 09:23