php - Laravel collection how to remove a specific key from all items?

Php - Laravel collection how to remove a specific key from all items?

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:

Using 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.
  • The modified items are returned as a new collection ($filteredCollection).

Using 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.
  • The modified items are converted back to arrays using toArray().

Notes:

  • 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.

Examples

  1. Laravel collection remove specific key from all items.

    • Description: This query seeks to remove a specific key from all items in a Laravel collection.
    • Code:
      <?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().
  2. Laravel collection unset key recursive.

    • Description: This query aims to unset a specific key recursively from all items in a nested Laravel collection.
    • Code:
      <?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.
  3. Laravel collection remove key and return new collection.

    • Description: This query seeks to create a new Laravel collection without a specific key in each item.
    • Code:
      <?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.
  4. Laravel collection forget key.

    • Description: This query aims to forget (remove) a specific key from all items in a Laravel collection using the forget() method.
    • Code:
      <?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.
  5. Laravel collection unset multiple keys.

    • Description: This query seeks to unset multiple keys from all items in a Laravel collection.
    • Code:
      <?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.
  6. Laravel collection remove key if exists.

    • Description: This query aims to remove a specific key from all items in a Laravel collection if it exists.
    • Code:
      <?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().
  7. Laravel collection pluck without specific key.

    • Description: This query seeks to create a new collection by plucking values excluding a specific key from each item.
    • Code:
      <?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.
  8. Laravel collection remove key from associative array.

    • Description: This query aims to remove a specific key from associative arrays within a Laravel collection.
    • Code:
      <?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.
  9. Laravel collection filter keys.

    • Description: This query seeks to filter keys to exclude a specific key from all items in a Laravel collection.
    • Code:
      <?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.
  10. Laravel collection unset key by index.

    • Description: This query aims to unset a specific key by index from all items in a Laravel collection.
    • Code:
      <?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().

More Tags

ignore-case unsafe-pointers methods undefined-index laravel-4 styling rowcount artificial-intelligence powerapps android-shapedrawable

More Programming Questions

More Statistics Calculators

More Biochemistry Calculators

More Biology Calculators

More Entertainment Anecdotes Calculators