laravel集合map方法为什么可以直接修改集合而不能修改数组呢?
代码
<?php
public function index()
{
$users = collect([
collect(['name' => '张三']),
collect(['name' => '李四']),
['name' => '王五']
]);
$users->map(function ($item, $key) {
$item['name'] .= 'xxxx';
});
dd($users, $new_users);
}
运行结果(图片传不上来了。。。)
Illuminate\Support\Collection {#388 ▼
#items: array:3 [▼
0 => Illuminate\Support\Collection {#424 ▼
#items: array:1 [▼
"name" => "张三xxxx"
]
#escapeWhenCastingToString: false
}
1 => Illuminate\Support\Collection {#425 ▼
#items: array:1 [▼
"name" => "李四xxxx"
]
#escapeWhenCastingToString: false
}
2 => array:1 [▼
"name" => "王五"
]
]
#escapeWhenCastingToString: false
}
对象传递始是引用的方式
参考 PHP: 对象和引用 - Manual