-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[5.4] refactoring mapWithKeys #16564
Conversation
I really don't understand why people think all these imbricated methods are simpler than a plain loop… Please see my revised version on #16552 for supporting multiple keys, as currently. |
Yep, this one looks simpler and lets you return multiple keys, i'll update the PR. And about the imbricated methods, they look so cool 😄 |
$this->assertEquals( | ||
[1, 3, 2], | ||
$data->keys()->all() | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assertSame
should be considered instead, as it does strict comparison thus also compares array orders. See Array Operators.
If there is an agreement to continue supporting multiple keys (I think it should), an unit test for this definitely should be added to prevent future regression. |
Changed to assertSame and added multiple keys tests accordingly =) |
Another overlook, callback should also receive the key: foreach ($this->items as $key => $value) {
$assoc = $callback($value, $key);
foreach ($assoc as $mapKey => $mapValue) {
$result[$mapKey] = $mapValue;
}
} I guess this should also be covered by tests… |
Is this ready to merge now? |
Yes, we should've covered all the possible usecases with this... |
Is there any chances this change made the mapWithKeys function stop working with multidimensional return? This worked fine in 5.3 and broke after I upgraded to 5.4
Used to return:
Now it only returns:
|
In your code above, the returned associative array always have a I guess you should remove that upper layer: return [
$item->index => [
$item->sub_index => [
$item->parameter => $item->value
]
]
]; |
@vlakoff Thanks for the reply. I tried but did not had any effect. I'm going to check the file history to see what I can find about it changing it's behavior, just wanted to check if there is a chance this commit has something to do about it. |
I just noticed you were merging the data (e.g. parameter1 and parameter2 in your example). I'm pretty sure your code was relying on some quirks of a previous implementation, so breakage could (and did) happen. TBH, you would be better using regular PHP functions and loops, rather than these obscure functions. |
This message by crynobone links to an article that may be of interest to you: array_merge_recursive() vs. array_replace_recursive(). |
Yes, I ended up solving it with a regular loop. As you previously stated:
Btw, thanks for the references. I'm going to take a look at them out of curiosity. |
Ping @vlakoff
With this change
mapWithKeys()
won't renumber the keys and will also maintain the order of the input array.Example:
When using the function:
Results into
Intead of the current behavior: