1
我可以使用array_map替換此代碼:如何使用帶回調函數的array_map作爲數組元素的成員?
$points = [];
foreach($addresses as $address) $points[] = $address->getRoutePoint();
$this->points = $points;
我可以使用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();
}