What is the best way for checking if an array is a flat one with primitive values or if its a multidimensional one? Is it possible to do it without iterating the array through a loop? Like, maybe by using is_array() on every element?
Nov 5, 2018 in Others 5,248 views

## 1 answer to this question.

Since the 'second dimension' could be just about anywhere, it isn't generally possible to do so without at least looping implicitly. But, let's say it happens to be in the first item, you could just do this:

`is_array(\$arr[0]);`

Otherwise, using a for loop that shortcircuits upon finding a match is probably the most efficient solution there is to it. After all, your own for loop is definitely way better than going with the implicit loop. So, here's how:

```\$ 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```

Now, here how you could do it with implicit looping, but you can't shortcircuit it right when you hit a match:

```\$ 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)```
• 8,240 points

## How to check if a cell is empty in a range variable?

Use WorksheetFunction.CountA() (https://learn.microsoft.com/en-us/office/vba/api/excel.worksheetfunction.counta) function If WorksheetFunction.CountA(rng) = 0 Then ...READ MORE

## How to check if DynamoDB table exists?

I'm a new user in boto3 and ...READ MORE

## How to find out if an item is present in an std::vector?

The most straightforward solution is to count the total number of elements in the vector that have the specified value.  If the count is greater than zero, we've found our element.  This is simple to accomplish with the std::count function. #include <iostream> #include <vector> #include <algorithm> int main() { ...READ MORE

## PHP multidimensional array search by value

I want to search for the uid ...READ MORE

## Implode array values?

My array: Array ( [0] => ...READ MORE

## PHP array mapping

Is there a cleaner way than foreach to get ...READ MORE