Search

Laravel Array Helper Function: array_forget

November 19, 2016 —John Koster

forget(&$array, $keys)

The forget helper method removes items from the given $array. The specified keys are express in dot notation.

Mutable Function

This function affects the original $array.

Assuming an array is defined as follows:

1<?php
2 
3use Illuminate\Support\Arr;
4 
5$anArray = [
6 'person' => [
7 'first_name' => 'Jane',
8 'last_name' => 'Doe'
9 ]
10];

We could remove the first_name value like so:

1<?php
2 
3Arr::($anArray, 'person.first_name');

Which would leave the array as follows:

1<?php
2 
3array(1) {
4 ["person"] array(1) {
5 ["last_name"] "Doe"
6 }
7}

Removing multiple items at once is as simple as passing an array as the second argument:

1<?php
2 
3Arr::forget($anArray, ['person.first_name', 'person.last_name']);

#array_forget(&$array, $keys)

The array_forget function is a shortcut to calling Arr::forget. This function is declared in the global namespace.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.