How to check if array is multidimensional or not

0 votes
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 by Bharani
• 4,660 points
5,945 views

1 answer to this question.

0 votes

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)
answered Nov 5, 2018 by DataKing99
• 8,250 points

Related Questions In Others

0 votes
1 answer

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

answered Jan 15, 2023 in Others by narikkadan
• 63,600 points
833 views
0 votes
0 answers

SEO - How to programmatically check if a website has been Banned by Google

i wanted to link a website to ...READ MORE

Feb 14, 2022 in Others by Kichu
• 19,040 points
384 views
0 votes
0 answers

How to check if DynamoDB table exists?

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

Mar 14, 2022 in Others by Edureka
• 13,690 points
2,028 views
0 votes
1 answer

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

answered May 27, 2022 in Others by Damon
• 4,960 points
14,114 views
0 votes
0 answers

PHP multidimensional array search by value

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

May 30, 2022 in PHP by Kichu
• 19,040 points
1,071 views
0 votes
0 answers

How to sum all column values in multi-dimensional array?

How can I add all the columnar ...READ MORE

Jun 19, 2022 in PHP by narikkadan
• 63,600 points
1,445 views
0 votes
0 answers

Implode array values?

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

Jun 27, 2022 in PHP by narikkadan
• 63,600 points
401 views
0 votes
0 answers

PHP array mapping

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

Jul 22, 2022 in PHP by narikkadan
• 63,600 points
351 views
0 votes
1 answer
0 votes
1 answer

Reducing sequences in an array of strings

I've written a C# app to solves ...READ MORE

answered Nov 2, 2018 in Others by DataKing99
• 8,250 points
908 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP