Checking if array is multidimensional or not?

PhpArraysLoopsMultidimensional ArrayElement

Php Problem Overview


  1. What is the most efficient way to check if an array is a flat array of primitive values or if it is a multidimensional array?
  2. Is there any way to do this without actually looping through an array and running is_array() on each of its elements?

Php Solutions


Solution 1 - Php

Use count() twice; one time in default mode and one time in recursive mode. If the values match, the array is not multidimensional, as a multidimensional array would have a higher recursive count.

if (count($array) == count($array, COUNT_RECURSIVE)) 
{
  echo 'array is not multidimensional';
}
else
{
  echo 'array is multidimensional';
}

This option second value mode was added in PHP 4.2.0. From the PHP Docs:

> If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array. count() does not detect infinite recursion.

However this method does not detect array(array()).

Solution 2 - Php

The short answer is no you can't do it without at least looping implicitly if the 'second dimension' could be anywhere. If it has to be in the first item, you'd just do

is_array($arr[0]);

But, the most efficient general way I could find is to use a foreach loop on the array, shortcircuiting whenever a hit is found (at least the implicit loop is better than the straight for()):

$ more multi.php
<?php

$a = array(1 => 'a',2 => 'b',3 => array(1,2,3));
$b = array(1 => 'a',2 => 'b');
$c = array(1 => 'a',2 => 'b','foo' => array(1,array(2)));

function is_multi($a) {
    $rv = array_filter($a,'is_array');
    if(count($rv)>0) return true;
    return false;
}

function is_multi2($a) {
    foreach ($a as $v) {
        if (is_array($v)) return true;
    }
    return false;
}

function is_multi3($a) {
    $c = count($a);
    for ($i=0;$i<$c;$i++) {
        if (is_array($a[$i])) return true;
    }
    return false;
}
$iters = 500000;
$time = microtime(true);
for ($i = 0; $i < $iters; $i++) {
    is_multi($a);
    is_multi($b);
    is_multi($c);
}
$end = microtime(true);
echo "is_multi  took ".($end-$time)." seconds in $iters times\n";

$time = microtime(true);
for ($i = 0; $i < $iters; $i++) {
    is_multi2($a);
    is_multi2($b);
    is_multi2($c);
}
$end = microtime(true);
echo "is_multi2 took ".($end-$time)." seconds in $iters times\n";
$time = microtime(true);
for ($i = 0; $i < $iters; $i++) {
    is_multi3($a);
    is_multi3($b);
    is_multi3($c);
}
$end = microtime(true);
echo "is_multi3 took ".($end-$time)." seconds in $iters times\n";
?>

$ php multi.php
is_multi  took 7.53565130424 seconds in 500000 times
is_multi2 took 4.56964588165 seconds in 500000 times
is_multi3 took 9.01706600189 seconds in 500000 times

Implicit looping, but we can't shortcircuit as soon as a match is found...

$ more multi.php
<?php

$a = array(1 => 'a',2 => 'b',3 => array(1,2,3));
$b = array(1 => 'a',2 => 'b');

function is_multi($a) {
    $rv = array_filter($a,'is_array');
    if(count($rv)>0) return true;
    return false;
}

var_dump(is_multi($a));
var_dump(is_multi($b));
?>

$ php multi.php
bool(true)
bool(false)

Solution 3 - Php

For PHP 4.2.0 or newer:

function is_multi($array) {
    return (count($array) != count($array, 1));
}

Solution 4 - Php

I think this is the most straight forward way and it's state-of-the-art:

function is_multidimensional(array $array) {
    return count($array) !== count($array, COUNT_RECURSIVE);
}

Solution 5 - Php

You can simply execute this:

if (count($myarray) !== count($myarray, COUNT_RECURSIVE)) return true;
else return false;

If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array.

If it's the same, means there are no sublevels anywhere. Easy and fast!

Solution 6 - Php

You could look check is_array() on the first element, under the assumption that if the first element of an array is an array, then the rest of them are too.

Solution 7 - Php

After PHP 7 you could simply do:

public function is_multi(array $array):bool
{
    return is_array($array[array_key_first($array)]);
}

Solution 8 - Php

All great answers... here's my three lines that I'm always using

function isMultiArray($a){
    foreach($a as $v) if(is_array($v)) return TRUE;
    return FALSE;
}

Solution 9 - Php

This function will return int number of array dimensions (stolen from here).

function countdim($array)
{
   if (is_array(reset($array))) 
     $return = countdim(reset($array)) + 1;
   else
     $return = 1;
 
   return $return;
}

Solution 10 - Php

I think you will find that this function is the simplest, most efficient, and fastest way.

function isMultiArray($a){
	foreach($a as $v) if(is_array($v)) return TRUE;
	return FALSE;
}

You can test it like this:

$a = array(1 => 'a',2 => 'b',3 => array(1,2,3));
$b = array(1 => 'a',2 => 'b');

echo isMultiArray($a) ? 'is multi':'is not multi';
echo '<br />';
echo isMultiArray($b) ? 'is multi':'is not multi';

Solution 11 - Php

Even this works

is_array(current($array));

If false its a single dimension array if true its a multi dimension array.

current will give you the first element of your array and check if the first element is an array or not by is_array function.

Solution 12 - Php

Don't use COUNT_RECURSIVE

click this site for know why

use rsort and then use isset

function is_multi_array( $arr ) {
rsort( $arr );
return isset( $arr[0] ) && is_array( $arr[0] );
}
//Usage
var_dump( is_multi_array( $some_array ) );

Solution 13 - Php

You can also do a simple check like this:

$array = array('yo'=>'dream', 'mydear'=> array('anotherYo'=>'dream'));
$array1 = array('yo'=>'dream', 'mydear'=> 'not_array');

function is_multi_dimensional($array){
    $flag = 0;
    while(list($k,$value)=each($array)){
        if(is_array($value))
			$flag = 1;
    }
	return $flag;
}
echo is_multi_dimensional($array); // returns 1
echo is_multi_dimensional($array1); // returns 0

Solution 14 - Php

Try as follows

if (count($arrayList) != count($arrayList, COUNT_RECURSIVE)) 
{
  echo 'arrayList is multidimensional';

}else{

  echo 'arrayList is no multidimensional';
}

Solution 15 - Php

In my case. I stuck in vary strange condition.
1st case = array("data"=> "name");
2nd case = array("data"=> array("name"=>"username","fname"=>"fname"));
But if data has array instead of value then sizeof() or count() function not work for this condition. Then i create custom function to check.
If first index of array have value then it return "only value"
But if index have array instead of value then it return "has array"
I use this way

 function is_multi($a) {
        foreach ($a as $v) {
          if (is_array($v)) 
          {
            return "has array";
            break;
          }
          break;
        }
        return 'only value';
    }

Special thanks to [Vinko Vrsalovic][1] [1]: https://stackoverflow.com/questions/145337/checking-if-array-is-multidimensional-or-not/145348#145348

Solution 16 - Php

I think this one is classy (props to another user I don't know his username):

static public function isMulti($array)
{
	$result = array_unique(array_map("gettype",$array));

	return count($result) == 1 && array_shift($result) == "array";
}

Solution 17 - Php

All the above methods are too complex for quick rolling out. If an array is flat, testing the first element should return a primitive e.g int, string e.t.c. If it is multidimensional, it should return an array. By extension, you can use this one liner fast and neat.

echo is_array(array_shift($myArray));

if this returns true, the array is multidimensional. Else it is flat. Just to note, it is very rare for arrays to have different dimensions e.g. if you are generating data from a model, it will always have the same type of multidimensional or flat structure that can be traversed by loops. machine made loops If it isn't, then you have custom built it by hand, which means you know where everything will be and it just works without needing to write a looping algorithm enter image description here

Solution 18 - Php

In addition to the previous answers and depending on the schema of the array you want to check:

function is_multi_array($array=[],$mode='every_key'){
	
	$result = false;

	if(is_array($array)){
		
		if($mode=='first_key_only'){
			
			if(is_array(array_shift($array))){
				
				$result = true;
			}
		}
		elseif($mode=='every_key'){
			
			$result = true;
			
			foreach($array as $key => $value){
				
				if(!is_array($value)){
					
					$result = false;
					break;
				}
			}
		}
		elseif($mode=='at_least_one_key'){
			
			if(count($array)!==count($array, COUNT_RECURSIVE)){
				
				$result = true;	
			}
		}
	}
	
	return $result;
}

Solution 19 - Php

Its as simple as

$isMulti = !empty(array_filter($array, function($e) {
                    return is_array($e);
                }));

Solution 20 - Php

$is_multi_array = array_reduce(array_keys($arr), function ($carry, $key) use ($arr) { return $carry && is_array($arr[$key]); }, true);

Here is a nice one liner. It iterates over every key to check if the value at that key is an array. This will ensure true

Solution 21 - Php

if($array[0]){
//enter your code 
}

Solution 22 - Php

if ( array_key_exists(0,$array) ) {

// multidimensional array

}  else {

// not a multidimensional array

}

*only to those arrays with numeric index

Solution 23 - Php

function isMultiArray(array $value)
{
    return is_array(reset($value));
}

Solution 24 - Php

The native print_r function returns a human-readable string. Just count the "Array" instances.

try...

substr_count(print_r([...array...], true), 'Array') > 1;

$a = array(1 => 'a',2 => 'b',3 => array(1,2,3));
$b = array(1 => 'a',2 => 'b');
$c = array(1 => 'a',2 => 'b','foo' => array(1,array(2)));
$d = array(array());
$e = array(1, array());
$f = array(array(), array());
$g = array("hello", "hi" => "hi there");
$h[] = $g;

var_dump(substr_count(print_r($a, true), 'Array') > 1);
...

//a: bool(true)
//b: bool(false)
//c: bool(true)
//d: bool(true)
//e: bool(true)
//f: bool(true)
//g: bool(false)
//h: bool(true)

On my box, "is_multi took 0.83681297302246 seconds in 500000 times"

Courtesy: Ruach HaKodesh

Solution 25 - Php

is_array($arr[key($arr)]); 

No loops, plain and simple.

Works also with associate arrays not only numeric arrays, which could not contain 0 ( like in the previous example would throw you a warning if the array doesn't have a 0. )

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWilcoView Question on Stackoverflow
Solution 1 - PhpzashView Answer on Stackoverflow
Solution 2 - PhpVinko VrsalovicView Answer on Stackoverflow
Solution 3 - PhpscronideView Answer on Stackoverflow
Solution 4 - PhpAndreasView Answer on Stackoverflow
Solution 5 - PhpPian0_M4nView Answer on Stackoverflow
Solution 6 - PhpGreg HewgillView Answer on Stackoverflow
Solution 7 - Phpuser11995521View Answer on Stackoverflow
Solution 8 - PhpJoeView Answer on Stackoverflow
Solution 9 - Phpcreedence.myopenid.comView Answer on Stackoverflow
Solution 10 - PhpRoboTamerView Answer on Stackoverflow
Solution 11 - PhpPriyankView Answer on Stackoverflow
Solution 12 - Phphendra1View Answer on Stackoverflow
Solution 13 - PhpPrashantView Answer on Stackoverflow
Solution 14 - PhpArshid KVView Answer on Stackoverflow
Solution 15 - PhpDarkcoderView Answer on Stackoverflow
Solution 16 - PhpAlfonso Fernandez-OcampoView Answer on Stackoverflow
Solution 17 - PhpNelson Emeka AmeyoView Answer on Stackoverflow
Solution 18 - PhpRafaSashiView Answer on Stackoverflow
Solution 19 - PhpMohd Abdul MujibView Answer on Stackoverflow
Solution 20 - PhpDorpoView Answer on Stackoverflow
Solution 21 - PhprthcupidView Answer on Stackoverflow
Solution 22 - Phpsudhin babuView Answer on Stackoverflow
Solution 23 - PhpjhonatanView Answer on Stackoverflow
Solution 24 - PhpRacer XView Answer on Stackoverflow
Solution 25 - PhpCarlosView Answer on Stackoverflow