Search

Laravel Array Helper Function: array_only

November 18, 2016 —John Koster

The only helper method is the logical opposite of the except (discussed in the article Laravel Array Helper Function: array_except) method.

The signature for the only helper method is:

only($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 retrieve only a user's first name like so:

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

With the resulting array looking like this:

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

#array_only($array, $keys)

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