Method to forget nested keys in collections #40749
Replies: 5 comments 3 replies
-
Hey, @aschmelyun You can achieve the same result with the following: $collection = collect([
[
'name' => 'taylor',
'framework' => 'laravel'
],
[
'name' => 'andrew',
'framework' => 'vue'
]
]);
$newCollection = [];
$collection->each(function ($element) use (&$newCollection) {
unset($element['name']);
$newCollection[] = $element;
});
// [['framework' => 'laravel'],['framework' => 'vue']] But, I think it's a lot complicated than Maybe @taylorotwell has another opinion? |
Beta Was this translation helpful? Give feedback.
{{title}}
{{editor}}'s edit
{{editor}}'s edit
-
This is a great idea! |
Beta Was this translation helpful? Give feedback.
-
This is a great idea! |
Beta Was this translation helpful? Give feedback.
{{title}}
{{editor}}'s edit
{{editor}}'s edit
-
one thing that i'm missing in laravel is the power of using something like a Hashpath Syntax from cakephp in collections, this is a real power feature not available in laravel and would allow to extract data from nested API responses like twitter into a handy collection. |
Beta Was this translation helpful? Give feedback.
-
This would be awesome. Also allowing it to remove all items with that key, as far as the nest goes! Not just the first or second level. |
Beta Was this translation helpful? Give feedback.
{{title}}
-
Hi all! Just wanted to see if this would be a desired addition. For collections, there's the
forget()
method that removes a specific key from a collection:However, if there's a collection with nested elements, forget needs some kind of traversal alongside of it. Instead, I was proposing adding a new collection method to more easily handle this:
Beta Was this translation helpful? Give feedback.
All reactions