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

Fix maxDimensions #7022

wants to merge 2 commits into from

Conversation

jadedcore
Copy link
Contributor

*** This also effects the 3.x branch. ***

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.

Example:

$data = array(
             0 => array(
                 0 => 'Some Value',
                 1 => array(
                     0 => 'Some other Value'
                 )
             ),
             1 => array(
                 0 => 'Some Value',
             )
         );

Returns 2 under the current version of maxDimensions.
Returns 3 with the changes proposed above.

Chris Valliere added 2 commits July 15, 2015 11:20
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.

Example:
$data = array(
			 0 => array(
				 0 => 'Some Value',
				 1 => array(
					 0 => 'Some other Value'
				 )
			 ),
			 1 => array(
				 0 => 'Some Value',
			 )
		 );

Returns 2 under the current version of maxDimensions.
Returns 3 with the changes proposed above.
Added comments for $count param
@markstory markstory added this to the 2.8.0 milestone Jul 15, 2015
@markstory
Copy link
Member

Shouldn't this be a bug fix for 2.6? As positioned now, this fix won't be released for 4-5 months as it is going into 2.8.

} 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.

@jadedcore
Copy link
Contributor Author

Sorry @markstory I am new to contributing to this repository. I chose the 2.8 branch because it was the newest version of the 2.x branch. Should I close this request and start it in the 2.6 branch?

@markstory
Copy link
Member

Targetting 2.7 might be best, I'm going to do the final 2.6 release tonight.

@jadedcore
Copy link
Contributor Author

Alright, I'll close this pull request. I'll resubmit it to the 2.7 branch tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants