Determine number of Dimensions in a PHP Array

0 votes
asked Jan 23, 2010 by brian

Is there a way to determine how many dimensions there are in a PHP array?

5 Answers

0 votes
answered Jan 23, 2010 by alix-axel

Nice problem, here is a solution I stole from the PHP Manual:

function countdim($array)
{
    if (is_array(reset($array)))
    {
        $return = countdim(reset($array)) + 1;
    }

    else
    {
        $return = 1;
    }

    return $return;
}
0 votes
answered Jan 23, 2010 by ghostdog74

you can try this:

$a["one"]["two"]["three"]="1";

function count_dimension($Array, $count = 0) {
   if(is_array($Array)) {
      return count_dimension(current($Array), ++$count);
   } else {
      return $count;
   }
}

print count_dimension($a);
0 votes
answered Jan 23, 2010 by symcbean

Like most procedural and object-oriented languages, PHP does NOT natively implement multi-dimensional arrays - it uses nested arrays.

The recursive function suggested by others are messy, but the nearest thing to an answer.

C.

0 votes
answered Jan 23, 2010 by goat

This one works for arrays where each dimension doesn't have the same type of elements. It may need to traverse all elements.

$a[0] = 1;
$a[1][0] = 1;
$a[2][1][0] = 1;

function array_max_depth($array, $depth = 0) {
    $max_sub_depth = 0;
    foreach (array_filter($array, 'is_array') as $subarray) {
        $max_sub_depth = max(
            $max_sub_depth,
            array_max_depth($subarray, $depth + 1)
        );
    }
    return $max_sub_depth + $depth;
}
0 votes
answered Sep 15, 2017 by leo-tahk

was corrected at Some issues with jumping from one function to another in a loop in php


This double function will go to the last dimension of each array in $a and when its not an array anymore it will echo the number of loops it did to get there with a delimiter |. The downside of this code is it only echoes and cannot be returned (in normal way).

function cc($b, $n)
{
    $n++.' ';
    countdim($b, $n);

}
function countdim($a, $n = 0)
{
    if(is_array($a))
    {
        foreach($a as $b)
        {
            cc($b, $n);
        }
    }else
    {
        echo $n.'|';
    }
}
countdim($a);

Here i made a function with return, but.. its a return from html then "GET" back to php on button click.. I dont know any other way to make it work.. so just name your array to $a and hit the button :/

$max_depth_var = isset($_REQUEST['max_depth_var']) ? $_REQUEST['max_depth_var'] : 0;
?>
<form id="form01" method="GET">
<input type="hidden" name="max_depth_var" value="<?php
function cc($b, $n)
{
    $n++.' ';
    bb($b, $n);
}
function bb($a, $n = 0)
{
    if(is_array($a))
    { 
        foreach($a as $b)cc($b, $n); 
    }else
    {
    echo $n.', ';
    };
}
bb($a); ?>">
<input type="submit" form="form01" value="Get max depth value">
</form><?php
$max_depth_var = max(explode(', ', rtrim($max_depth_var, ",")));
echo "Array's maximum dimention is $max_depth_var.";
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...