Alex Rivera | Logout

Check if a multidimensional array has any non-empty leaf nodes

Asked 2010-10-25T12:03:39.360
15

How can I check an array recursively for empty content like this example:

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

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

        )

)

The array is not empty but there is no content. How can I check this with a simple function?

Edit
Report

2 Answers

0
$arr=array_unique(array_values($args));
if(empty($arr[0]) && count($arr)==1){
 echo "empty array";
}
answered 2013-03-04T07:54:06.723
0

Returns TRUE if passed a variable other than an array, or if any of the nested arrays contains a value (including falsy values!). Returns FALSE otherwise. Short circuits.

function has_values($var) {
  if (is_array($var)) {
    if (empty($var)) return FALSE;
    foreach ($var as $val) {
      if(has_values($val)) return TRUE;
    }
    return FALSE;
  } 
  return TRUE;
}
answered 2013-05-12T15:15:19.947

Your Answer