Skip to content

Commit

Permalink
Fix Hash::maxDimensions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Chris Valliere authored and markstory committed Jul 21, 2015
1 parent 7b2d7ad commit 64f0ca0
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions lib/Cake/Utility/Hash.php
Expand Up @@ -756,18 +756,15 @@ public static function dimensions(array $data) {
* 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);
Expand Down

0 comments on commit 64f0ca0

Please sign in to comment.