如何将array_map与回调函数作为数组元素的成员使用



我可以使用array_map替换此代码:

$points = [];
foreach($addresses as $address) $points[] = $address->getRoutePoint();
$this->points = $points;

没什么特别的:

$this->points = array_map(function($i) { return $i->getRoutePoint(); }, $addresses);

但是,使用foreach更快,您不需要使用$points变量:

$this->points = [];
foreach($addresses as $address) {
    $this->points[] = $address->getRoutePoint();
}

最新更新