mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-03 10:02:42 +00:00
Fix Hash::maxDimensions
The current Hash::maxDimensions function calls Hash::dimensions to try to get the maximum depth of the passed in array. However, this ends up only getting the depth of the first element of each 1st dimension element in the array passed to maxDimensions. The function needs to be called recursively in order to get the depth of ALL of the elements in all of the dimensions of the passed in array. I made the maxDimensions function more closely resemble the deprecated Set::countDim function in order to restore the correct functionality.
This commit is contained in:
parent
7b2d7ad748
commit
64f0ca0028
1 changed files with 4 additions and 7 deletions
|
@ -756,18 +756,15 @@ class Hash {
|
|||
* number of dimensions in a mixed array.
|
||||
*
|
||||
* @param array $data Array to count dimensions on
|
||||
* @param int $count counts current depth of this iteration
|
||||
* @return int The maximum number of dimensions in $data
|
||||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::maxDimensions
|
||||
*/
|
||||
public static function maxDimensions(array $data) {
|
||||
$depth = array();
|
||||
public static function maxDimensions($data, $count = 0) {
|
||||
$depth = array($count);
|
||||
if (is_array($data) && reset($data) !== false) {
|
||||
foreach ($data as $value) {
|
||||
if (is_array($value)) {
|
||||
$depth[] = self::dimensions($value) + 1;
|
||||
} else {
|
||||
$depth[] = 1;
|
||||
}
|
||||
$depth[] = static::maxDimensions($value, $count + 1 );
|
||||
}
|
||||
}
|
||||
return empty($depth) ? 0 : max($depth);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue