Is there a way to determine how many dimensions there are in a PHP 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)) .
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.
Multidimensional PHP arrays can have any number of dimensions, starting from two. Two-dimensional PHP arrays contain arrays in which variables are stored.
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.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With