Arr::get

Posted on November 16, 2016

The Arr::get function retrieves a value from a deeply nested array using "dot" notation.

The Arr::get function also accepts a default value, which will be returned if the specific key is not found:

$value = Arr::get($array, 'names.john', 'default');

Example usage

$array = ['products' => ['desk' => ['price' => 100]]];

$value = Arr::get($array, 'products.desk');

// ['price' => 100]

$array1 = [
    ['id' => 1, 'name' => 'John Doe'],
    ['id' => 2, 'name' => 'Jane Doe']
];

$result = Arr::get($array1, '1.name');

// Jane Doe

$array1 = [
    ['id' => 1, 'name' => 'John Doe'],
    ['id' => 2, 'name' => 'Jane Doe']
];

$result = Arr::get($array1, '11.name', [1,2,3]);

// [1,2,3]

Parameters

\ArrayAccess|array $array

string $key

mixed $default

Return Value

mixed

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *