Skip to content

[12.x] Add keyByListValues array helper #55892

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

Closed
wants to merge 3 commits into from

Conversation

daniser
Copy link
Contributor

@daniser daniser commented May 30, 2025

This PR adds new keyByListValues array helper function.
This function keys array list entries by its values while preserving its associative part, thus making array 100% associative.
Newly keyed entries' values are replaced with some default value (2nd argument).
The caller is responsible for list entries to be valid array keys.

(I don't know if terminology is correct because AFAIK arrays can be either associative or non-associative, not both)

Suggested use case is when some optional parameter(s) may be associated with array entry (likely class name or some identifier):

$middleware = [
    EnsureUserHasRole::class => ['role' => 'editor'],
    SomeOtherMiddleware::class,
];

Arr::keyByListValues($middleware, []);

/*[
    EnsureUserHasRole::class => ['role' => 'editor'],
    SomeOtherMiddleware::class => [],
];*/

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

daniser pushed a commit to daniser/framework that referenced this pull request May 30, 2025
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.

2 participants