How to remove the key from an array with Laravel?
This tutorial is on How to remove the key from an array with Laravel? Many times we require to delete the specific keys from the array-like password and confirm-password field. we don’t require to store the confirm-password field. So we have to drop that key from the input array, At that time we always use PHP pre-define unset() function. And Laravel 6, Laravel 7, and Laravel 8 provide you can remove the key from the array helper.
Example 1
Arr::except
use removes the given key/value pairs from the array.
use Illuminate\Support\Arr; $userArray = ['name'=>'Devnote','email'=>'devnote16@gmail.com','password'=>'abc@998877','confirm-password'=>'abc@998877']; $userArray = Arr::except($userArray,['confirm-password']); print_r($userArray); Output: Array ( [name] => Devnote [email] => devnote16@gmail.com [password] => abc@998877 )
Example 2
We can remove multiple keys from variables using Laravel pre-define array_except()
function.
$userArray = ['name'=>'Devnote','email'=>'devnote16@gmail.com','password'=>'abc@998877','confirm-password'=>'abc@998877']; $userArray = array_except($userArray,['confirm-password']); print_r($userArray); Output: Array ( [name] => Devnote [email] => devnote16@gmail.com [password] => abc@998877 )
Call to undefined function app/http/controller/array_except()
All str_ and array_ helpers have been moved to the new laravel/helpers
Composer package and removed from the framework in the new version 6.
Now you can add a helpers package:
composer require laravel/helpers
And you added the package try dump-autoload:
composer dump-autoload