在PHP中,在给定原始顺序时,对已排序的对象数组进行分类的最佳方法是什么?
Let's say I have one array of ID numbers in a desired particular order. I have a second array of objects that were sorted by an ID property from the first array.
$array1 = [3,4,2,1,5];
$array2 = [
["id" => 1, "data" => "one"],
["id" => 2, "data" => "two"],
["id" => 3, "data" => "fre"],
["id" => 4, "data" => "foe"],
["id" => 5, "data" => "fie"]
];
In PHP, what is the best way of 'unsorting' or reverting the second array to the original order of the first array?
The closest answer I can find without using a sort is:
$array1_flipped = array_flip($array1);
$array2_unsorted = array();
for($i = 0; $i < count($array1); $i++) {
$array2_unsorted[$array1_flipped[$array2[$i]['id']]] = $array2[$i];
}
return($array2_unsorted);
Edit: For those interested, here is how the question arose. The first array is a list of IDs to be displayed in a particular order. The second array is the return of the MySQL call WHERE id IN $array2
, which is returned sorted. However, the second array needs to be resorted back into the order of the first array. Due to size issues, I was hoping to be able to remap the second array using the keys and values of the first array without sorting.
假设我有一个特定顺序的ID号数组。 我有第二个对象数组,它们是由第一个数组的ID属性排序的。 p>
$ array1 = [3,4,2,1,5];
$ array2 = [
[“id”=&gt; 1,“data”=&gt; “one”],
[“id”=&gt; 2,“数据”=&gt; “two”],
[“id”=&gt; 3,“data”=&gt; “fre”],
[“id”=&gt; 4,“数据”=&gt; “敌人”],
[“id”=&gt; 5,“data”=&gt; “fie”]
];
code> pre>
在PHP中,将第二个数组“解除”或恢复到第一个数组的原始顺序的最佳方法是什么? ? p>
我能找到的最接近的答案是: p>
$ array1_flipped = array_flip($ array1);
\ n $ array2_unsorted = array();
for($ i = 0; $ i&lt; count($ array1); $ i ++){
$ array2_unsorted [$ array1_flipped [$ array2 [$ i] ['id' ]]] = $ array2 [$ i];
}
return($ array2_unsorted);
code> pre>
编辑 strong>:对于 那些感兴趣的人,这就是问题是如何产生的。 第一个数组是要按特定顺序显示的ID列表。 第二个数组是MySQL调用 WHERE id IN $ array2 code>的返回,返回排序。 但是,需要将第二个数组恢复到第一个数组的顺序。 由于大小问题,我希望能够使用第一个数组的键和值重新映射第二个数组而不用 em>排序。 p>
div>
I found the solution by introducing a third array and using a method similar to Gauss-Jordan elimination. While this is beautiful, I wish there was a one-step algorithm for this. I'll award the correct answer to anyone who finds it.
$array1 = [3,4,2,1,5];
$array2 = [
["id" => 1, "data" => "one"],
["id" => 2, "data" => "two"],
["id" => 3, "data" => "fre"],
["id" => 4, "data" => "foe"],
["id" => 5, "data" => "fie"]
];
// Placeholder sorted ascending array (e.g. $array3 = [1,2,3,4,5])
$array3 = range(1,count($array1));
array_multisort($array1, $array3);
// Now $array3 = [4,3,1,2,5], the inverse map of an $array1 sort
array_multisort($array3, $array2);
return $array2;
usort
with an anonymous function receiving the order array via the use
keyword:
$order = [3,4,2,1,5];
$ar = [
["id" => 1, "data" => "one"],
["id" => 2, "data" => "two"],
["id" => 3, "data" => "fre"],
["id" => 4, "data" => "foe"],
["id" => 5, "data" => "fie"]
];
usort($ar, function($a, $b) use ($order) {
$ai = array_search($a['id'], $order);
$bi = array_search($b['id'], $order);
if($ai == $bi) return 0;
return ($ai < $bi) ? -1 : 1;
});
print_r($ar);