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
4,050 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,240 points

Related Questions In Others

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 in Others by Kichu
• 19,040 points
64 views
0 votes
0 answers

How to check if DynamoDB table exists?

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

Mar 14 in Others by Edureka
• 13,640 points
760 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 in Others by Damon
• 4,960 points
53 views
0 votes
1 answer

How is machine learning used to detect objects?

To understand the surrounding environment in real ...READ MORE

answered Nov 30, 2018 in Others by Kailash
310 views
0 votes
0 answers

PHP multidimensional array search by value

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

May 30 in PHP by Kichu
• 19,040 points
187 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 in PHP by narikkadan
• 21,680 points
136 views
0 votes
0 answers

Implode array values?

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

Jun 27 in PHP by narikkadan
• 21,680 points
35 views
0 votes
0 answers

PHP array mapping

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

Jul 22 in PHP by narikkadan
• 21,680 points
22 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,240 points
279 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