By John Koster
The only
method can be used to retrieve a subset of the provided array. The subset of the returned array is based on the values supplied to the $key
parameter.
#Signature
The signature of the only
method is:
1public static function only(
2 $array,
3 $keys
4);
#Example Use
The only
helper method is the logical opposite of the except
method. Using the same $_POST
array from the previous example, we could retrieve only a user's first name like so:
1use Illuminate\Support\Arr;
2
3$inputData = Arr::only(
4 $_POST,
5 'first_name'
6 );
With the resulting array looking like this:
1array {
2 ["first_name"] "John"
3}
#Global array_only
Helper Function
The array_only
function is a shortcut to calling Arr::only
. This function is declared in the global namespace.
∎