Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix maxDimensions #7022

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 4 additions & 7 deletions lib/Cake/Utility/Hash.php
Original file line number Diff line number Diff line change
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 current depth count for this interation of the function
* @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 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this recursively walk every branch in a nested array and count the maxDimensions each time? While that might yield the right answer, I'm not sure it is the most efficient way to derive that answer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this is the most efficient way. I pieced the code together from the deprecated Set::countDim function primarily because the current Hash::maxDimensions function is not returning the correct value. I discovered this while trying to migrate from the deprecated Set functions to the new Hash functions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this more, there isn't really a more efficient way. To find the max depth we have to do a full tree traversal as there is no way of knowing where the deepest leaf is.

}
}
return empty($depth) ? 0 : max($depth);
Expand Down