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:
Chris Valliere 2015-07-16 08:50:32 -04:00 committed by mark_story
parent 7b2d7ad748
commit 64f0ca0028

View file

@ -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);