lithium\util\Set::depth()
Counts the dimensions of an array. If $all
is set to false
(which is the default) it will
only consider the dimension of the first element in the array.
Parameters
-
array
$data
Array to count dimensions on.
-
array
$options
Returns
integerThe number of dimensions in $array
.
Source
public static function depth($data, array $options = []) {
$defaults = ['all' => false, 'count' => 0];
$options += $defaults;
if (!$data) {
return 0;
}
if (!$options['all']) {
return (is_array(reset($data))) ? static::depth(reset($data)) + 1 : 1;
}
$depth = [$options['count']];
if (is_array($data) && reset($data) !== false) {
foreach ($data as $value) {
$depth[] = static::depth($value, [
'all' => $options['all'],
'count' => $options['count'] + 1
]);
}
}
return max($depth);
}