1

Here is my array anyone can help would be appreciate. i want to check if array has any value on it or all values are empty. multidimensional array is dynamic either it can be index of 0 or more than 0.....n . but can this possible to check without for loop ?

 Array
 (
    [0] => Array
         (
             [title1] => 
             [title2] => 
             [image] => 
         )
    [1] => Array
         (
             [title1] => 
             [title2] => 
             [image] => 
         )
    [2] => Array
         (
             [title1] => SampleTitle
             [title2] => 
             [image] => 
         )
)
Tejas Soni
  • 551
  • 3
  • 10

2 Answers2

1
$filtered = array_filter($my_array);
if (!empty($filtered)) {
  // your code
}
1

You can do something like this: source for flatten from @too much php.

$arr=Array
 (
    '0' => Array
         (
             'title1' => '',
             'title2' => '',
             'image' => ''
         ),
    '1' => Array
         (
             'title1' => '',
             'title2' => '',
             'image' => ''
         ),
    '2' => Array
         (
             'title1' => 'SampleTitle',
             'title2' => '',
             'image' => ''
         )
);

function flatten(array $array) {
    $return = array();
    array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
    return $return;
}

$str=flatten($arr);
if(!array_filter($str)) {
    echo "all are null";
}else{
    echo "values are there";
}
Community
  • 1
  • 1
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44