mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-03 10:02:42 +00:00
Adding support for Set::countDim() to count dimensions in all branches of an array, not just the first element. Set second parameter of countDim to true to get the real dimension of the array (regardless if the deepest depth is on first element).
Adding better documentation and test cases for countDim git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5960 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
4e163f471f
commit
9b01ce7066
2 changed files with 60 additions and 5 deletions
|
@ -598,22 +598,35 @@ class Set extends Object {
|
|||
return true;
|
||||
}
|
||||
/**
|
||||
* Counts the dimensions of an array.
|
||||
* 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.
|
||||
*
|
||||
* @param array $array Array to count dimensions on
|
||||
* @param boolean $all Set to true to count the dimension considering all elements in array
|
||||
* @param integer $count Start the dimension count at this number
|
||||
* @return integer The number of dimensions in $array
|
||||
* @access public
|
||||
*/
|
||||
function countDim($array = null) {
|
||||
function countDim($array = null, $all = false, $count = 0) {
|
||||
if ($array === null) {
|
||||
$array = $this->get();
|
||||
} elseif (is_object($array) && is_a($array, 'set')) {
|
||||
$array = $array->get();
|
||||
}
|
||||
if (is_array(reset($array))) {
|
||||
$return = Set::countDim(reset($array)) + 1;
|
||||
if ($all) {
|
||||
$depth = array($count);
|
||||
if (is_array($array) && reset($array) !== false) {
|
||||
foreach ($array as $value) {
|
||||
$depth[] = Set::countDim($value, true, $count + 1);
|
||||
}
|
||||
}
|
||||
$return = max($depth);
|
||||
} else {
|
||||
$return = 1;
|
||||
if (is_array(reset($array))) {
|
||||
$return = Set::countDim(reset($array)) + 1;
|
||||
} else {
|
||||
$return = 1;
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue