Laravel 8 data_get() {#collection-method}
data_get
函数使用「.」符号从多维数组或对象中根据指定键检索值:
$data = ['products' => ['desk' => ['price' => 100]]];
$price = data_get($data, 'products.desk.price');
// 100
data_get
函数也接受一个默认值,如果没有找到指定的键,将返回默认值:
$discount = data_get($data, 'products.desk.discount', 0);
// 0
这个函数也可以使用 「*」 作为通配符匹配键名:
$data = [
'product-one' => ['name' => 'Desk 1', 'price' => 100],
'product-two' => ['name' => 'Desk 2', 'price' => 150],
];
data_get($data, '*.name');
// ['Desk 1', 'Desk 2'];