views:

90

answers:

2

hello!

how can i check a array recursively of empty content like this example

Array
(
    [product_data] => Array
        (
            [0] => Array
                (
                    [title] => 
                    [description] => 
                    [price] => 
                )

        )
    [product_data] => Array
        (
            [1] => Array
                (
                    [title] => 
                    [description] => 
                    [price] => 
                )

        )

)

The Array is not Empty but there is no Content? How can i Check this with a easy Function?

Thank!!

+2  A: 

Assuming the array will always contain the same type of data:

function TestNotEmpty($arr) {
    foreach($arr as $item)
        if(isset($item->title) || isset($item->descrtiption || isset($item->price))
            return true;
    return false;
}
David Mårtensson
+3  A: 
emurano
A more general and fault tolerant solution than mine :), but could you not short circuit it and return immediately on a non empty answer instead of continuing to test the rest of the array?
David Mårtensson
foreach ($InputVariable as $Value)
comod
@David Martensson: The execution will only be routed to that else statement if the variable given to the function is not an array. Think of the array given in the question as a tree and the else block as the base case that all leaves will be evaluated by.
emurano
Yes, but if it is an array it will complete the array and every array higher in the hierarchy even though it might already have found that its not empty. His example clearly specified that you could have empty rows but they should still be considered empty.
David Mårtensson
Ah I see what you're saying. Yes, you're right. Once you find a non-empty element you should stop comparing and return false. My implementation will needlessly continue to check for non-empty elements until it's traversed the whole structure.
emurano