PHP的数组排序基于另一个数组? [英] PHP-Sort array based on another array?
本文介绍了PHP的数组排序基于另一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
OK,我已经得到了在计算器这个问题,但遗憾的是它在JavaScript - <一个href=\"http://stackoverflow.com/questions/13304543/javascript-sort-array-based-on-another-array\">Javascript - 基于另一个数组排序阵列
OK, I already got this question in stackoverflow but sadly it's in javascript - Javascript - sort array based on another array
,我希望它在PHP
$data = array(
"item1"=>"1",
"item2"=>"3",
"item3"=>"5",
"item4"=>"2",
"item5"=>"4"
);
要匹配这个阵列的排列方式:
to match the arrangement of this array:
sortingArr = array("5","4","3","2","1");
和我在寻找的输出:
$data = array(
"item3"=>"5",
"item5"=>"4",
"item2"=>"3",
"item4"=>"2",
"item1"=>"1"
);
不知道如何可以做到这一点?
谢谢你。
Any idea how this can be done? Thanks.
推荐答案
pretty简单?
$data = array(
"item1"=>"1",
"item2"=>"3",
"item3"=>"5",
"item4"=>"2",
"item5"=>"4"
);
$sortingArr = array("5","4","3","2","1");
$result = array(); // result array
foreach($sortingArr as $val){ // loop
$result[array_search($val, $data)] = $val; // adding values
}
print_r($result); // print results
输出:
Array
(
[item3] => 5
[item5] => 4
[item2] => 3
[item4] => 2
[item1] => 1
)
这篇关于PHP的数组排序基于另一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文