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

[8.x] Add undot() method to Arr helpers and Collections #39729

Merged
merged 4 commits into from Nov 26, 2021
Merged

[8.x] Add undot() method to Arr helpers and Collections #39729

merged 4 commits into from Nov 26, 2021

Conversation

amiranagram
Copy link
Contributor

@amiranagram amiranagram commented Nov 23, 2021

This PR adds support for elevate method in array helpers and collections.

This method basically does the opposite of Arr::dot(), it takes an associative array and converts it to a multi-dimensional one based on keys with dot notation.

For example if we take this array and pass it through Arr::dot():

$original = [
    'user' => [
        'name' => 'foo',
        'occupation' => 'bar',
    ]
];

$dotted = Arr::dot($original);

We would get a flattened associative array:

$dotted = [
    'user.name' => 'foo',
    'user.occupation' => 'bar',
];

Now if we elevate the dotted array we should get the original.

$elevated = Arr::elevate($dotted);

$elevated ≡ $original

Same goes for collections and lazy collections. Not sure if "elevate" is the best name for it, so I'm open to suggestions.

Cheers.

P.S. Will take a look on how to test if a collection is actually lazy and add an additional test. Some help is welcome, of course.

@GrahamCampbell GrahamCampbell changed the title Add elevate() method to Arr helpers and Collections [8.x] Add elevate() method to Arr helpers and Collections Nov 23, 2021
@johanrosenson
Copy link
Contributor

johanrosenson commented Nov 24, 2021

This has been suggested before as undot, which i personally think is a great name, pretty self explanatory.

#38167
#33845
#13897
#4735
#4292

sadly undot have not been merged so far.

@amiranagram
Copy link
Contributor Author

@johanrosenson Welp, wasn't aware that this was suggested before.

Agree, undot sounds way better. Elevate is an antonym of flatten as far as I know and flatten is something else entirely.

@taylorotwell taylorotwell changed the title [8.x] Add elevate() method to Arr helpers and Collections [8.x] Add undot() method to Arr helpers and Collections Nov 26, 2021
@taylorotwell
Copy link
Member

Since this keeps coming back up as a PR I will go ahead and include it.

@taylorotwell taylorotwell merged commit 78637dc into laravel:8.x Nov 26, 2021
@amiranagram amiranagram deleted the elevate-collection-arr-method branch November 26, 2021 15:48
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

3 participants