如何将带有回调函数的array_map用作数组元素的成员?

时间:2017-09-23 17:55:53

标签: php

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

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

1 个答案:

答案 0 :(得分:0)

没什么特别要做的:

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

但是,使用foreach的速度更快,您无需使用$points变量:

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