In Laravel, you can remove a specific key from all items in a collection using the map() method along with PHP's unset() function or Laravel's forget() method. Here's how you can achieve this:
map() and unset()If you have a collection and you want to remove a specific key from each item:
use Illuminate\Support\Collection; // Example collection $collection = collect([ ['id' => 1, 'name' => 'John', 'age' => 30], ['id' => 2, 'name' => 'Jane', 'age' => 25], ['id' => 3, 'name' => 'Doe', 'age' => 40], ]); // Key to remove $keyToRemove = 'age'; // Remove the key from each item in the collection $filteredCollection = $collection->map(function ($item) use ($keyToRemove) { unset($item[$keyToRemove]); return $item; }); // Output the filtered collection $filteredCollection->all(); In this example:
map() iterates over each item in the collection.unset($item[$keyToRemove]) removes the specified key ('age' in this case) from each item.$filteredCollection).map() and forget()Alternatively, you can use Laravel's forget() method to remove the key:
use Illuminate\Support\Collection; // Example collection $collection = collect([ ['id' => 1, 'name' => 'John', 'age' => 30], ['id' => 2, 'name' => 'Jane', 'age' => 25], ['id' => 3, 'name' => 'Doe', 'age' => 40], ]); // Key to remove $keyToRemove = 'age'; // Remove the key from each item in the collection $filteredCollection = $collection->map(function ($item) use ($keyToRemove) { return collect($item)->forget($keyToRemove)->toArray(); }); // Output the filtered collection $filteredCollection->all(); In this approach:
forget($keyToRemove) removes the specified key ('age' in this case) from the collection item.toArray().Immutable Operations: Both methods (unset() and forget()) create a new collection with modified items, leaving the original collection intact.
Chaining: You can chain other collection methods after map() if you need further transformations or operations.
Performance Consideration: If you have a large collection, consider the performance implications of creating a new collection with map(). In such cases, you might prefer modifying the original array directly using a loop.
By using map() along with unset() or forget(), you can effectively remove a specific key from all items in a Laravel collection according to your needs. Adjust the key and collection variable names as per your specific use case.
Laravel collection remove specific key from all items.
<?php // Assuming $collection is your Laravel collection $collection->each(function ($item) { unset($item['specific_key']); }); Description: This code iterates through each item in the collection using each() and removes the specific_key using unset().Laravel collection unset key recursive.
<?php // Function to unset a key recursively in a Laravel collection function unsetRecursive(&$item, $keyToRemove) { unset($item[$keyToRemove]); foreach ($item as &$value) { if (is_array($value) || $value instanceof \Traversable) { unsetRecursive($value, $keyToRemove); } } } // Usage $collection->each(function (&$item) { unsetRecursive($item, 'specific_key'); }); Description: This code defines a recursive function unsetRecursive() to unset the specific_key from nested arrays or collections within the main collection.Laravel collection remove key and return new collection.
<?php // Assuming $collection is your Laravel collection $newCollection = $collection->map(function ($item) { unset($item['specific_key']); return $item; }); Description: This code uses map() to iterate over each item in the collection, unset specific_key, and returns a new collection $newCollection.Laravel collection forget key.
forget() method.<?php // Assuming $collection is your Laravel collection $collection->each(function ($item) { $item->forget('specific_key'); }); Description: This code uses each() to iterate over each item in the collection and forget() to remove specific_key.Laravel collection unset multiple keys.
<?php // Assuming $collection is your Laravel collection $keysToRemove = ['key1', 'key2', 'key3']; $collection->each(function ($item) use ($keysToRemove) { foreach ($keysToRemove as $key) { unset($item[$key]); } }); Description: This code iterates through each item in the collection and unsets multiple keys specified in $keysToRemove.Laravel collection remove key if exists.
<?php // Assuming $collection is your Laravel collection $collection->each(function ($item) { if (isset($item['specific_key'])) { unset($item['specific_key']); } }); Description: This code checks if specific_key exists in each item using isset() and removes it if present using unset().Laravel collection pluck without specific key.
<?php // Assuming $collection is your Laravel collection $newCollection = $collection->map(function ($item) { return collect($item)->except(['specific_key'])->all(); }); Description: This code uses map() to iterate over each item, except() to exclude specific_key, and all() to convert the resulting collection back to an array.Laravel collection remove key from associative array.
<?php // Assuming $collection is your Laravel collection $collection->each(function (&$item) { if (is_array($item)) { unset($item['specific_key']); } }); Description: This code checks if each item is an array using is_array() and removes specific_key if it exists.Laravel collection filter keys.
<?php // Assuming $collection is your Laravel collection $filteredCollection = $collection->map(function ($item) { return collect($item)->filter(function ($value, $key) { return $key !== 'specific_key'; })->all(); }); Description: This code uses map() to iterate over each item, filter() to exclude specific_key, and all() to convert the resulting collection back to an array.Laravel collection unset key by index.
<?php // Assuming $collection is your Laravel collection $indexToRemove = 1; // Example index $collection->each(function (&$item) use ($indexToRemove) { $keys = array_keys($item->toArray()); if (isset($keys[$indexToRemove])) { $item->forget($keys[$indexToRemove]); } }); Description: This code iterates through each item in the collection, retrieves keys using array_keys(), and removes the key at $indexToRemove using forget().ignore-case unsafe-pointers methods undefined-index laravel-4 styling rowcount artificial-intelligence powerapps android-shapedrawable