Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine number of Dimensions in a PHP Array

Tags:

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

like image 997
Brian Avatar asked Jan 23 '10 05:01

Brian


People also ask

How do you find the number of dimensions in an array?

The number of dimensions in an array is the same as the length of the size vector of the array. In other words, ndims(A) = length(size(A)) .

How do I find the size of an array in PHP?

We can use the PHP count() or sizeof() function to get the particular number of elements or values in an array. The count() and sizeof() function returns 0 for a variable that we can initialize with an empty array. If we do not set the value for a variable, it returns 0.

How many dimensions can there be in an array PHP?

Multidimensional PHP arrays can have any number of dimensions, starting from two. Two-dimensional PHP arrays contain arrays in which variables are stored.

How many dimensions are in an array?

More than Three Dimensions Although an array can have as many as 32 dimensions, it is rare to have more than three. When you add dimensions to an array, the total storage needed by the array increases considerably, so use multidimensional arrays with care.


1 Answers

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; } 
like image 69
Alix Axel Avatar answered Oct 04 '22 14:10

Alix Axel