Arr::get

Reads a value from a nested array by dot-notation path, returning a default when it is missing.

Signature
public static function get($array, $key, $default = null)
  • Added in Laravel 5.0

Parameters

ParameterTypeDescription
$$array\ArrayAccess|arrayThe array or ArrayAccess object to read from. Anything else immediately yields the default.
$$keystring|int|nullThe key or dot-notation path to read. Passing null returns the entire array.
$$defaultoptionalmixed= nullReturned when the path does not exist. A Closure is invoked only at that point.

Return value

mixedThe value at the given path, the whole array when the key is null, or the resolved default when the path is missing.

Arr::get reads a value out of a nested array by a dot-notation path and hands back a default when any segment along the way is missing. It never emits an undefined-index warning, which is the main reason to prefer it over bracket chains when reading data you did not create.

When to reach for it

Use it for anything shaped like an external payload: a decoded JSON body, a webhook, a third-party API response, a config array. It also accepts any ArrayAccess object, so it works over collections and similar containers, and passing null as the key returns the whole array — handy when the path is a variable that may be empty.

What trips people up

It does not support the * wildcard. That belongs to the global data_get() helper; Arr::get($data, 'items.*.name') simply returns the default. Before splitting on dots, it first checks whether the whole string is an existing key, so a literal 'a.b' key wins over the nested path. And the default runs through value(), so a Closure default is executed rather than returned.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Reading a deeply nested field out of a decoded webhook body without warnings.
  • Supplying a fallback for an optional setting in a config array.
  • Reading a path held in a variable, where an empty path should yield the whole array.

Frequently asked questions

Does Arr::get support the * wildcard?

No, only the global data_get() helper expands wildcards; Arr::get treats * as an ordinary key segment and returns the default.

What is the difference between Arr::get and data_get?

Arr::get works on arrays and ArrayAccess with plain dot paths, while data_get also traverses objects and supports * wildcards across every element at a level.

Why did Arr::get return my default when the key clearly exists?

Usually the value is stored under a key containing a literal dot, or the path passes through a value that is not an array, in which case the traversal stops and the default is returned.

More examples

No wildcards: use data_get

Prints NULL then ["Desk","Chair"]. The two helpers look interchangeable and are not: only data_get expands * into every element at that level, and it also traverses objects, which Arr::get does not.

Example · php
<?php

use Illuminate\Support\Arr;

$data = ['products' => [['name' => 'Desk'], ['name' => 'Chair']]];

// Arr::get treats * as a literal segment and misses.
var_dump(Arr::get($data, 'products.*.name'));

// data_get understands the wildcard.
echo json_encode(data_get($data, 'products.*.name')), "\n";

A literal dotted key beats the nested path

Prints "literal" then "nested". The whole key is tested with array_key_exists before the string is split on dots, so a key that genuinely contains a dot shadows the nested path of the same name. This matters for data keyed by domain names, versions or filenames.

Example · php
<?php

use Illuminate\Support\Arr;

$array = [
    'a.b' => 'literal',
    'a' => ['b' => 'nested'],
];

echo Arr::get($array, 'a.b'), "\n";
echo Arr::get($array['a'], 'b'), "\n";

Reading an optional field from a webhook

Prints {"amount":2500,"currency":"usd"}. The present field is read through three levels in one expression and the absent one falls back to its default without a warning, which is the pattern to use for any payload whose shape you do not control.

Example · php
<?php

use Illuminate\Support\Arr;

$body = json_decode('{"data":{"object":{"amount":2500}}}', true);

$amount = Arr::get($body, 'data.object.amount', 0);
$currency = Arr::get($body, 'data.object.currency', 'usd');

echo json_encode(compact('amount', 'currency')), "\n";

Discussion

  • Be the first to comment on this lesson.