April 11, 2018 —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.
The signature of the only
method is:
1public static function only(2 $array,3 $keys4);
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}
array_only
Helper FunctionThe array_only
function is a shortcut to calling Arr::only
. This function is declared in the global namespace.
∎
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.