Arr::forget() | Laravel Helper Function

  • 15-05-2022
  • 1520
  • Laravel 9
  • Haresh Chauhan

Laravel Arr::forget() helper use when you need to remove key from array, or you can say remove key from given array key / value also you can remove nested array also deeply using dot notation.

This laravel helper function especially needs you you need to update data in the database or sometimes you now want to update data when it going to update that time using Arr::forget() you can remove that array key or nested array key also can remove using dot notation.

Here below four different example given how to use array forget in laravel with four different example.

You can also called unset key from array. in core php you were used unset() function that's here Arr::forget(), Here benefit is you can forget nester array key / valie pair also while in unset() function you can't.

So let's see following example how to use Arr::forget() laravel helper function in development.

Exmaple 1: Use with simple forget

Here is a simple array forget example, Were forget the value name from array.

use Illuminate\Support\Arr;

$array = [
  'name' => 'Jhone',
  'age' => 22,
  'country' => 'us'
];
     
Arr::forget($array, 'name');

// OUTPUT

array:2 [▼
  "age" => 22
  "country" => "us"
]

Exmaple 2: Use with nested array forget

You must need to use Arr before forgetting the function and also need to define the arr function on call in your controller. Here is simple array forget example, customer and it's subscriber, through using Arr::forget() erase the subscriber customer, nested array.

use Illuminate\Support\Arr;

$array = ['customer' => ['subscriber' => ['price' => 100]]];
     
Arr::forget($array, 'customer.subscriber');

// OUTPUT

array:1 [▼
  "customer" => []
]

Exmaple 3: Use with multiple dimensional nested array forget

This is multiple dimensional array, Using array forget the Jhone second brother Hussy forget from the array.

use Illuminate\Support\Arr;

$array = [
  'name' => 'Jhone',
  'age' => 26,
  'brother' => [
      'Mickey' => [
          'age' => 22,
          'occupation' => 'manager',
      ],
      'Hussy' => [
          'age' => 24,
          'occupation' => 'business-manager',
      ],
  ],
  'father' => 'Mr. Jhordan',
  'mother' => 'Mrs. Jhordan'
];
     
Arr::forget($array, 'brother.Hussy');

// OUTPUT

array:5 [▼
  "name" => "Jhone"
  "age" => 26
  "brother" => array:1 [▼
    "Mickey" => array:2 [▼
      "age" => 22
      "occupation" => "manager"
    ]
  ]
  "father" => "Mr. Jhordan"
  "mother" => "Mrs. Jhordan"
]

Exmaple 4: Use with multiple dimensional nested array key forget

In this exmaple has multiple dimensional array, using Arr::forget() Jhone second brother is hussy, age forget from his brother details.

$array = [
  'name' => 'Jhone',
  'age' => 26,
  'brother' => [
      'Mickey' => [
          'age' => 22,
          'occupation' => 'manager',
      ],
      'Hussy' => [
          'age' => 24,
          'occupation' => 'business-manager',
      ],
  ],
  'father' => 'Mr. Jhordan',
  'mother' => 'Mrs. Jhordan'
];

Arr::forget($array, 'brother.Hussy.age');

array:5 [▼
  "name" => "Jhone"
  "age" => 26
  "brother" => array:2 [▼
  "Mickey" => array:2 [▼
    "age" => 22
    "occupation" => "manager"
    ]
  "Hussy" => array:1 [▼
    "occupation" => "business-manager"
    ]
  ]
  "father" => "Mr. Jhordan"
  "mother" => "Mrs. Jhordan"
]

This is above different example, you can easy to understand how to use Arr::forget() laravel helper function in laravel, special use when update time image key forget if you don't want to update.


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :