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

[11.x] Add optional ability to array wrap associative arrays #52473

Closed
wants to merge 1 commit into from
Closed

[11.x] Add optional ability to array wrap associative arrays #52473

wants to merge 1 commit into from

Conversation

ctwillie
Copy link

@ctwillie ctwillie commented Aug 13, 2024

These additions allow array wrapping for associative arrays. Currently, Arr::wrap() does not wrap an associative array, but simply returns that same array. This feature is fully backwards compatible because the wrapping of associative arrays is opt in.. by passing true to Arr::wrap as the $wrapIfAssoc argument. This defaults to false which maintains current wrapping behavior.

Use Case

Functions that define an array parameter could be passed a list or an associative array. Also, many users implement functions that could be used on 1 data set or many data sets.. i.e. associative arrays. Being able to wrap associative arrays produces slightly more concise code and convenience.

Here, $params could either be ['id' => 1] or [['id' => 1], ['id' => 2]]...

Before

function (array $params) {
    $params = array_is_list($params) ? $params : [$params];

    foreach ($params as $param) {
        // process param
    }
}

After

function (array $params) {
    foreach (Arr::wrap($params, true) as $param) {
        // process param
    }
}

@ctwillie ctwillie changed the title Add optional ability to array wrap associative arrays [11.x] Add optional ability to array wrap associative arrays Aug 14, 2024
@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!

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