PHP排序多维数组由其他数组 [英] PHP sort multidimensional array by other array
本文介绍了PHP排序多维数组由其他数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有ID的数组,看起来像
I have an array with IDs that looks like
array(
0 => 12
1 => 30
2 => 50
3 => 11
4 => 22
5 => 45
[...]
)
和另一个多维数组,看起来像
and another multidimensional array that looks like
array(
0 => array(
'id' => 12,
'title' => 'title 12',
),
1 => array(
'id' => 50,
'title' => 'title 50',
),
2 => array(
'id' => 11,
'title' => 'title 11',
),
3 => array(
'id' => 30,
'title' => 'title 30',
),
4 => array(
'id' => 45,
'title' => 'title 45',
),
5 => array(
'id' => 22,
'title' => 'title 22',
),
)
第二阵列的id对应于第一数组中的值。我需要做的排序是通过在顺序子阵列的ID它们是第一阵列中的第二个阵列。
The ids of the 2nd array correspond to the values in the first array. What I need to do is sort the 2nd array by the IDs of the sub-arrays in the order they are in the 1st array.
什么是做到这一点的最好方法是什么?
What's the best way to do it?
推荐答案
假设用的ID阵列被命名为 $为了
并与该数组被命名为 $项目
:
Assuming the array with the IDs is named $order
and the array with the values is named $items
:
$keys = array_flip($order);
usort($items, function($a, $b) use($keys)
{
return $keys[$a['id']] - $keys[$b['id']];
});
这篇关于PHP排序多维数组由其他数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文