我有一个带有这些值的数组
array(
0 => 'D003',
1 => 'P001',
2 => 'P002',
3 => 'D001'
);
我有另一个带有这些值的多维数组B
array(
0 =>
array(
'waypoint_id' => 'D001',
'count' => '5'),
1 =>
array(
'waypoint_id' => 'D003',
'count' => '1'),
2 =>
array(
'waypoint_id' => 'P001',
'count' => '2'),
3 =>
array(
'waypoint_id' => 'P002',
'count' => '1')
);
如何使用array_multisort以便我可以让我的数组B像这样(与数组B的顺序相同)
array(
0 =>
array(
'waypoint_id' => 'D003',
'count' => '1'),
1 =>
array(
'waypoint_id' => 'P001',
'count' => '2'),
2 =>
array(
'waypoint_id' => 'P002',
'count' => '1'),
3 =>
array(
'waypoint_id' => 'D001',
'count' => '5')
);
答案 0 :(得分:2)
我不确定array_multisort()
可以在这里帮到你,因为A的内容没有明显的顺序。
我是通过路由点纯粹为了效率索引B,然后从A创建一个新的$ordered
数组:
$indexed = array();
foreach($b as $array) {
$indexed[$array['waypoint_id']] = $array;
}
$ordered = array();
foreach($a as $waypoint) {
$ordered[] = $indexed[$waypoint];
}
答案 1 :(得分:0)
$result = array();
foreach ($A as $search) {
foreach ($B as $elt) {
if ($elt['waypoint_id'] === $search) {
array_push($result, $B);
break;
}
}
}
var_dump($result);