Skip to content

Add setIfExists() to Arr #55489

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 2 commits into from
Closed

Conversation

ziming
Copy link
Contributor

@ziming ziming commented Apr 20, 2025

Usage

Example 1:

$person = ['name' => 'Jane'];

Arr::setIfExists($person, 'price', 888);

This returns you ['name' => 'Jane'] as it will not be set as the 'price' key does not exist in the array

Example 2:

$people = ['name' => 'Jane'];

Arr::setIfExists($person, 'name', 'Jack');

This returns you ['name' => 'Jack'] as the key exist in the original array

I think this method can be quite convenient if you want to iterate over an array to replace certain PIIs with faker data only if the array contain certain json keys but otherwise you do not want to add new keys to the original payload array

@rodrigopedra
Copy link
Contributor

Illuminate\Support\Arr is macroable. One can add this method to their project if needed.

If the maintainers believe this should be added, having a symmetric Arr::setIfDoesntExist() would be a good addition too.

The argument is similar to this PR, when one wants to be sure that all records, in a record set, have the same fields available, and potentially fill missing fields with default values, for example, for an export.

Either way, in my opinion, I would add both cases as macros to a project which needs either of those.

@ziming
Copy link
Contributor Author

ziming commented Apr 21, 2025

Arr::set behave the same way as setIfDoesntExists hence why I add this setIfExists

maybe Arr::setOnlyIfExists is a better method name than Arr::setIfExists

@rodrigopedra
Copy link
Contributor

Arr::set behave the same way as setIfDoesntExists hence why I add this setIfExists

No. Maybe I wasn't clear when explaining.

Arr::set() replaces the value, even if exists previously, a hypothetic Arr::setIfDoesntExists() would only set the value if it doesn't previously exist.

For example, if one wants to make sure all records have the same fields available:

$recordset = [
    ['name' => 'John'],
    ['name' => 'Mary', 'age' => 21],
];

foreach($records as $record) {
   Arr::setIfDoesntExists($record, 'name', 'NA');
   Arr::setIfDoesntExists($record, 'age', 'NA');
}

print_r($recordset);

/*
$recordset = [
    ['name' => 'John', 'age' => 'NA'],
    ['name' => 'Mary', 'age' => 21],
];
*/

Arr::set(), in the case above, would replace all values with NA.

@rodrigopedra
Copy link
Contributor

Well, as I said before, I think both options are niche, and could be added as macros to a project if needed.

The example, from my previous comment, could be replaced with the null-coalescing assignment:

foreach($records as $record) {
   $record['name'] ??= 'NA';
   $record['age'] ??= 'NA';
}

Unless one want to preserve pre-existing null values, and only fill in missing values. Then again, it could be added as a macro to a project which needs such method.

@AndrewMast
Copy link
Contributor

AndrewMast commented Apr 21, 2025

Do we have Fluent for arrays? I always forget and there isn't much on Fluent besides for strings on the docs. It would be cool if we could do something like

Arr::whenExists($arr, 'key')->set('key', 'value');

or

Arr::fluent($arr)->whenExists('key')->set('key', 'value');

@Rizky92
Copy link

Rizky92 commented Apr 21, 2025

Do we have Fluent for arrays?

We have Collections, Arr helper is only complimentary for niche array manipulation.

Regarding the PR, I don't think it's feasible anyway to add more stuff in Arr helper unless its use case doesn't necessarily involve somewhat complex array manipulation. As @rodrigopedra said, you could macro this to your own.

@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!

@ziming ziming deleted the add-setIfExists-to-Arr branch April 21, 2025 19:26
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.

5 participants