Is it possible to know if a two-dimensional array is empty?

4

Basically my doubt is already cleared in the question. That's it, I wonder if can you tell if a two-dimensional array is empty?

Thank you in advance!

    
asked by anonymous 19.05.2016 / 02:03

1 answer

2

Yes, it is possible.

In this example I make a loop in the array revenant and print all the arrays that compose it, which are not empty, that is, print all except the last array that makes up the "main" array ".

So you just need to check the array key with the empty() of PHP if there are values associated with the inner array.

Example:

<?php

$revenant = array(
        'cast' => array(
            'Leading Role' => 'Leonardo DiCaprio',
            'Supporting Actor' => 'Tom Hardy'
        ),
        'overview' => array(
            'Movie' => 'The Revenant',
            'Director' => 'Alejandro Iñárritu',
            'Year' => '2015'
        ),
        'details' => array()
);

foreach($revenant as $key => $val){
    if(!empty($val)){
        print_r($val);
    }
}
    
19.05.2016 / 04:11