1

Possible Duplicate:
Is there a way to find how how “deep” a PHP array is?

I am trying to write a method to count the number of dimensions of an array. The following gives me a correct count of dimensions

$array = array();
$array[0] = array();
$array[0][0] = 0;
$array[0][1] = array();
$array[0][1][0] = 10;
$array[0][1][1] = 11;

echo '<p>'.\utility\arrayTools\arrayTools::numberOfDimensions($array).'</p>';
//3 Dimensons

The second examples also gives me a correct count of the number of dimensions

$array = array();
$array[0] = array();
$array[0][0] = 0;
$array[0][1] = array();
$array[0][1][0] = 10;
$array[0][1][1] = 11;
$array[1] = 1;
$array[2] = 2;
//3 Dimensions

But the following example gives me too high of a count

$array = array();
$array[0] = array();
$array[0][0] = 0;
$array[0][1] = array();
$array[0][1][0] = 10;
$array[0][1][1] = 11;
$array[1] = 1;
$array[2] = 2;
$array[3] = array();
$array[3][0] = 30;
//Should still be 3 dimensions, but gives me 4

The method I am using is below

//Method
public static function numberOfDimensions($array)
{
    if(func_num_args() === 2){
        if(is_int(func_get_arg(1))){
            $number_of_dimensions = func_get_arg(1);
        }else{
            throw new Exception('The second argumment must be an interger');
        }
    }else{
        $number_of_dimensions = 0;
    }

    if(is_array($array) === TRUE){
        $number_of_dimensions++;

        if(self::isMultiDimensional($array) === TRUE){
            foreach($array as $iteration){
                $number_of_dimensions = self::numberOfDimensions($iteration,$number_of_dimensions);
            }
            return $number_of_dimensions;
        }else{
            return $number_of_dimensions;
        }
    }else{
        return $number_of_dimensions;
    }
}

I already know the problem is it is still adding for every multidimensional even though the count may be equal to or less then the number of dimensions. But what I can't figure out is how to get it to find the highest number of dimensions and stop counting

Community
  • 1
  • 1
JoeyD473
  • 2,890
  • 2
  • 21
  • 25
  • 1
    nice array_depth function here: http://stackoverflow.com/questions/262891/is-there-a-way-to-find-how-how-deep-a-php-array-is – Varon Nov 12 '12 at 23:16
  • Dimensions may not be consistent...you might have a top-index with a different amount of array tree level than another one. What happens in this case? Minimum, maximum or average? – Christian Nov 12 '12 at 23:17
  • Please get out of the habit of doing `if (x == true)`, it adds needless clutter to your code. Just use `if (x)`. It is most egregious when you use `===` and explicitly test against uppercase `TRUE`, as in `if(is_array($array) === TRUE){`. Just use `if (is_array($array))`. – user229044 Nov 12 '12 at 23:17
  • Also, ever branch of your nested if/else structure uses `return $number_of_dimensions;` You should remove all but the last one and get rid of the final `else`. – user229044 Nov 12 '12 at 23:19
  • @meagar `id(x===true)` is not the same as `if(x)`. Admittedly, `is_array` _does_ always return a boolean. – John Dvorak Nov 12 '12 at 23:20
  • @JanDvorakq That is my point. He also uses `=== TRUE` with a method prefixed with `is`, which presumably also always returns boolean true/false. – user229044 Nov 12 '12 at 23:22
  • Here's a cleaned up version of your code. I'm not saying it's *correct* or that I've fixed it, it simply has a bunch of the inexperienced coding practices removed. It's cleaner, clearer and half the length http://pastebin.com/tghjwVec – user229044 Nov 12 '12 at 23:23
  • @Varon TY for the link. When I searched that question never came up. – JoeyD473 Nov 13 '12 at 17:15
  • @meager I normally don't do is_array($var) === true. I honestly don't know why I did it in this function. – JoeyD473 Nov 13 '12 at 17:16

4 Answers4

1

Here is a simpler version of your script

function numberOfDimensions($array) {
    $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
    $d = 0;
    foreach ( $it as $v )
        $it->getDepth() >= $d and $d = $it->getDepth();
    return ++ $d;
}
Baba
  • 94,024
  • 28
  • 166
  • 217
0

This function assumes it is being called with an array as parameter, thus the minimum depths is 1.

It then recursively looks at all child elements, and if the child is deeper than already known, it add the depth of the child to the calculated depth:

function numberOfDimensions($subject) {
    // scalar value has depth 0
    if(!is_array($subject)) return 0;

    // array has min depth of 1
    $depth = 1;
    foreach ($subject as $element) {
        if (is_array($element)) {
            // is the sub array deeper than already known?
            $sub_depth = numberOfDimensions($element);
            if ($sub_depth >= $depth) {
                $depth += $sub_depth;
            }
        }
    }
    return $depth;
}
Kaii
  • 20,122
  • 3
  • 38
  • 60
0

From another answer here on SO:

function array_depth($array) {
    $max_depth = 1;

    foreach ($array as $value) {
        if (is_array($value)) {
            $depth = array_depth($value) + 1;

            if ($depth > $max_depth) {
                $max_depth = $depth;
            }
        }
    }

    return $max_depth;
}
Community
  • 1
  • 1
Joshua Dwire
  • 5,415
  • 5
  • 29
  • 50
0

I havn't written any PHP in a very long while, so there may very well be mistakes in my code, excuse me in advance.

function numberOfDimensions($array,$so_far=0){
    if !is_array($array){
        return 0;
    }
    $max_dims = 0;
    foreach($array as $element){
        $element_dims = numberOfDimensions($array);
        $max_dims = max($max_dims,$element_dims);
    }
    return $max_dims +1;
}
bigblind
  • 12,539
  • 14
  • 68
  • 123