Laravel map():如何改变对象和数组?
2022-08-30 20:55:21
我有一个多维收藏。我想迭代它并使用map()函数更改它的一些子对象和数组:https://laravel.com/docs/5.1/collections#method-map
示例内容:
[
{
'address': 'Somestreet 99'
'orders': [
{'id': 11},
{'id': 67}
]
}
]
例
$deliveries = $delivery_addresses->map(function($delivery_address){
$orders = $delivery_address->orders->filter(function($order){
return $order->id == 67;
});
$delivery_address['address'] = 'A different street 44'
$delivery_address['orders'] = $orders;
$delivery_address['a_new_attribute'] = 'Some data...';
return $delivery_address;
});
预期结果:
[
{
'address': 'A different street 44'
'orders': [
{'id': 67}
],
'a_new_attribute': 'Some data...;
}
]
结果是,只有字符串类型变量将被更改。任何数组或对象都将保持不变。这是为什么,如何绕过它?谢谢!=)