Dim*_*ims 0 php closures hashmap
我写了这个
$result = array();
array_map(function($row) use ($result) {
$result[$row->id] = array();
$result[$row->id]['geojson'] = $row->geojson;
}, $regions);
Run Code Online (Sandbox Code Playgroud)
并且最后$result是空的.
是否有可能以这种方式填充数组?
$result函数内部是外部数组的副本,因此您所做的更改不会影响原始数组.您需要使用参考:use (&$result)
array_map(function($row) use (&$result) {
$result[$row->id] = array();
$result[$row->id]['geojson'] = $row->geojson;
}, $regions);
Run Code Online (Sandbox Code Playgroud)
或者你可以简单地使用 foreach
foreach ($regions as $row) {
$result[$row->id] = array();
$result[$row->id]['geojson'] = $row->geojson;
}
Run Code Online (Sandbox Code Playgroud)