Checking If Array Is Multidimensional or Not

Checking if array is multidimensional or not?

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)

Check if an array is multi-dimensional

Use count twice, one with single parameter, and one with recursive mode

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

count(array,mode)

  • array ---Required. Specifies the array or object to count.
  • mode ---Optional. Specifies the mode of the function. Possible values:

    • 0 - Default. Does not detect multidimensional arrays (arrays within arrays)
    • 1 - Detects multidimensional arrays

Note: This parameter was added in PHP 4.2

Check if array is single or multidimensional

A multidimensional array is simply an array where each of the items are arrays.
You can check if an array has sub-arrays in it by:

if (b.getClass().getComponentType().isArray()) {
...
}

Then you can do it recursively.

public void check(Object[] b, int... indices) {
if (b.getClass().getComponentType().isArray()) {
//check sub-arrays
int[] i2 = Arrays.copyOfRange(indices, 1, indices.length);
check(b[0], i2);
}
if (indices[0] > b.length)
throw new ArrayIndexOutOfBoundsException("Out of Bounds");
}

How to check if an array is multidimensional

Try the following:

import numpy as np

my_array = np.array([[1,2,3],[4,5,6]])
d = len(my_array.shape)
print(d) # Output: 2

Now, you can test against d, if its value is 2, then your array is 2 dimensions.

check if numpy array is multidimensional or not

Use the .ndim property of the ndarray:

>>> a = np.array([[ -7.94627203e+01,  -1.81562235e+02,  -3.05418070e+02,  -2.38451033e+02],[  9.43740653e+01,   1.69312771e+02,   1.68545575e+01,  -1.44450299e+02],[  5.61599000e+00,   8.76135909e+01,   1.18959245e+02,  -1.44049237e+02]])
>>> a.ndim
2

How to check if array is multidimensional? (jQuery)

You need to check the first element of Array so use

if(arr[0].constructor === Array)

DEMO

alert("[[]] returns " + ([[]].constructor === Array))

test if array is multidimensional

Assuming you meant

var ratings = [[1], [2]];

as var ratings = [1], [2]; is a syntax error, you could do

ratings.filter(Array.isArray).length

to get the number of arrays inside the wrapping array (2)

FIDDLE

PHP check if multidimensional array(array([key]= value)) values are empty

array_map('array_filter', $array) will remove all inner empty values, bringing the array to something like:

[
51 => [],
78 => [
18 => 'not empty'
]
]

Then array_filter that again to remove all empty arrays. In summary:

if (!array_filter(array_map('array_filter', $array))) {
echo 'The array is completely empty';
}


Related Topics



Leave a reply



Submit