Laravel Array Helper Function: array_except

November 18, 2016 —John Koster

The except helper method will return all the key/value pairs of the $array where the keys in the original array are not in the $keys array.

The signature for the except helper method is:

except($array, $keys)

Assuming that the $_POST super-global contains the following information:

1array(3) {
2 ["first_name"] "John"
3 ["last_name"] "Doe"
4 ["password"] "some_password"
5}

We could easily get all the information except for the password like so:

1<?php
2 
3use Illuminate\Support\Arr;
4 
5$inputData = Arr::except($_POST, 'password');

$inputData would then contain the following items:

1array(2) {
2 ["first_name"] "John"
3 ["last_name"] "Doe"
4}

When passing a single item, such as password, for the $keys parameter, it will be converted to an array automatically.

Passing an array would look like this:

1<?php
2 
3use Illuminate\Support\Arr;
4 
5// This would only return an array with the user's `last_name`.
6$inputData = Arr::except($_POST, ['password', 'first_name']);

#array_except($array, $keys)

The array_except function is a shortcut to calling Arr::except. 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.