我有三个数组,我想将它们与多维关联数组组合。原始数组$statiosnIds = [12, 17, 20, 32];$distances = [0, 2.5, 3.0, 6.2];$orders = [0, 1, 2, 3];结果查看$data = [ [ 'station_id' => 12, 'distance' => 0, 'order' => 0, ], [ 'station_id' => 17, 'distance' => 2.5, 'order' => 1, ], [ 'station_id' => 20, 'distance' => 3.0, 'order' => 2, ], [ 'station_id' => 32, 'distance' => 6.2, 'order' => 3, ]];我可以为每个循环使用三个来完成此操作,但我想知道是否有更好、更优化的方法来实现它。
1 回答
MYYA
TA贡献1868条经验 获得超4个赞
你可以使用array_map
代码看起来像这样
$data = array_map(function($a, $b, $c) {
return ['station_id' => $a, 'distance' => $b, 'order' => $c];
}, $statiosnIds, $distances, $orders);
- 1 回答
- 0 关注
- 104 浏览
添加回答
举报
0/150
提交
取消