If you have an array within an array and you want to get a value from it you are unable to due to the get method not using the Arr helper class.
<?php
use Illuminate\Support\Arr;
Route::get('/', function () {
$array = [
'hello' => [
'world' => 'abc123'
]
];
$collection = collect($array)->get('hello.world');
$array = Arr::get($array, 'hello.world');
dd($collection, $array); // will return null, abc123
});
Change the get method to:
public function get($key, $default = null)
{
return Arr::get($this->items, $key, $default);
}
@themsaid Could we have more information about why you don't want that?
Several issues and PR about that was closed, and I can't understand why Collection::get() should be different than Arr::get().
Lot of methods in Collection use data_get, so we can use dot notation in where, or pluck but not in has or get??
It feels weird..!
Just hit this same problem, no consistency here.
Collection is an object that gives you extra powers in dealing with arrays, there's no need
to mix functions that deal with pure arrays.
Collection implements ArrayAccess, so you can do array_get on a Collection.
$collection = collect([
'firstLevel' => 'valueFirstLevel',
'multi' => [
'value' => 'yea'
],
]);
array_get('multi.value', $collection); // 'yea'
Most helpful comment
Just hit this same problem, no consistency here.