Detecting a Multi-Dimensional Array in PHP

Posted on December 9th, 2011 by Wayne May in PHP | 0 comments

I recently had to find a quick way to check if an array is a multi-dimensional array, only to find that there are no PHP built-in function to do this. Here is a quick and easy way to do it:

1
2
3
4
function is_multi_array( array $arr )
{
    return is_array($arr[key($arr)]);
}

This can easily be used in the following context:

6
7
8
9
10
$a = array("dimension1" => array("dimension2" => "I like it") );
if( is_multi_array( $a ) )
{
    echo $a['dimension1']['dimension2'];
}

Post a comment

Hello guest, care to post a comment?