- 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?
- Is there any way to do this without actually looping through an
array and running
is_array()
on each of its elements?
-
11It's worth pointing out that PHP does not have true multi-dimensional arrays -- just simple associative array's of values. So your question is really asking "is there a non-scalar value in my array"?– gahooaCommented May 17, 2010 at 2:50
-
26Actually... I don't think that's worth pointing out at all.– JoeCommented Jul 26, 2013 at 2:14
16 Answers
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())
.
-
5
-
This answer is provably incorrect and the post score indicates that MANY researchers have been misled. 3v4l.org/ptGQ9 Commented Jul 18, 2022 at 22:46
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)
-
4Good, with the caveat that I believe that your filtering line should have array_map("is_array",$a), not using is_array as a bareword. Commented Sep 28, 2008 at 6:54
-
Good catch, that sped up is_multi, but still not good enough to match foreach Commented Sep 28, 2008 at 6:59
-
3It is worth noting that, as written, multi_3 will only work on zero-based non-associative arrays with no gaps in the indices, meaning it won't correctly identify any of these examples as multi-dimensional. Commented Mar 19, 2016 at 19:50
-
In function
is_multi()
optimize the code by doingreturn count($rv)>0
Commented Sep 2, 2016 at 16:50 -
is_array(array_values($arr)[0]) as a workaround for customized keys. Commented Jan 18, 2019 at 13:55
For PHP 4.2.0 or newer:
function is_multi($array) {
return (count($array) != count($array, 1));
}
-
1Not working for
array(array())
orarray(array(), array())
either. Generally, if an inside array is empty then the recursive count will correctly add 0 for it, thus making it match the normal count. Commented Oct 5, 2016 at 13:13 -
1
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);
}
-
This unreliable technique was already recommended by multiple answers on this page years earlier. This answer adds no new value to the page. Commented Jul 18, 2022 at 22:42
After PHP 7 you could simply do:
public function is_multi(array $array):bool
{
return is_array($array[array_key_first($array)]);
}
-
1You just check the first array element if it is an array. What if it is not but the 2nd element is an array? For example
$array = ['0' => 0, '1' => ['0' => 1]];
This is a multidim array but your function says false.– aProggerCommented Mar 21, 2022 at 15:17
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.
-
That's actually a good point. In my particular case, it's an either/or situation since I am controlling the creation of the original array. I'll leave the question open for now in case there's a solution that might work more generally though.– WilcoCommented Sep 28, 2008 at 6:27
-
5Like this:
if( is_array(current($arr)) ) { // is multidimensional }
Commented Mar 31, 2014 at 13:51 -
This is only a valid option if the first level of the array has consistent data types. Commented Jul 18, 2022 at 22:36
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';
-
if you're going to one-line it at least do the whole thing;
foreach($a as $v) is_array($v) ? return TRUE : return FALSE;
Commented Aug 29, 2017 at 14:06 -
@RobertPounder or even
foreach($a as $v) return is_array($v) ? true : false;
Commented Dec 7, 2019 at 19:49 -
1Uuuuuuumm, no, RobertPounder and YassineSedrani. You can't one-liner it like that; that would create different logic. Returning on the first iteration does not check the whole array. You might as well just evaluate on the first element if you are going to unconditionally return early. No. For a fully reliable solution, you need to allow the possibility of iterating the full array as a pathway to determining the opposite data type. Commented Jul 18, 2022 at 22:27
Don't use COUNT_RECURSIVE
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 ) );
-
$arr[0] could not be and array but $arr[1] could be an array Commented Nov 27, 2018 at 17:29
-
2This answer has a larger time complexity than is necessary because sorting is more expensive than merely iterating every element in the array. I would not recommend this answer for any reason. Commented Jul 18, 2022 at 22:41
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.
-
2That won't be reliable if you want to ensure that any other element isn't nested either. Commented Dec 21, 2017 at 17:05
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
-
1This answer is not optimized because it does not conditionally return early. Commented Jul 18, 2022 at 22:24
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;
}
-
This will only work for Greg's case. It's not a general solution to the problem where the second dimension could be anywhere in the array Commented Sep 28, 2008 at 6:38
-
$arr = array("hello", "hi" => "hi there"); $arr[] = &arr; //oops– TyzoidCommented Jul 8, 2013 at 14:12
-
This answer does not check multiple elements in each level. This answer is very untrustworthy for this asked question. Commented Jul 18, 2022 at 22:30
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";
}
-
This is not going to be a performant solution --
array_map()
does not allow short circuiting, so the FULL array will be iterated. There are other solutions on this page that are better than this one. Commented Jul 18, 2022 at 22:16
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
-
1This code is not reliable; it only checks the first element. Why
break
inside of the condition if you are going tobreak
immediately after the condition? Why use anybreak
s if you are only going toreturn
afterbreak
ing? This is just another verbose version of other earlier answers on this page that do not do a great job of evaluating the whole array. Commented Jul 18, 2022 at 22:35
Its as simple as
$isMulti = !empty(array_filter($array, function($e) {
return is_array($e);
}));
-
This is not going to be a performant solution --
array_filter()
does not allow short circuiting, so the FULL array will be iterated. There are other solutions on this page that are better than this one. It is needless to call theempty()
function when the passed in value is guaranteed to exist.!empty()
can be replaced with a function-less type cast(bool)
. Commented Jul 18, 2022 at 22:18
$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
-
This is not going to be a performant solution --
array_reduce()
does not allow short circuiting, so the FULL array will be iterated. There are other solutions on this page that are better than this one. Commented Jul 18, 2022 at 22:15
Try as follows
if (count($arrayList) != count($arrayList, COUNT_RECURSIVE))
{
echo 'arrayList is multidimensional';
}else{
echo 'arrayList is no multidimensional';
}