基于另一个数组的PHP排序数组?

前端之家收集整理的这篇文章主要介绍了基于另一个数组的PHP排序数组?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好的,我已经在stackoverflow中遇到了这个问题,但遗憾的是它在 javascriptJavascript – sort array based on another array

我想在PHP

$data = array(
   "item1"=>"1","item2"=>"3","item3"=>"5","item4"=>"2","item5"=>"4"
);

匹配这个数组的排列:

sortingArr = array("5","4","3","2","1");

和我正在寻找的输出

$data = array(
    "item3"=>"5","item5"=>"4","item1"=>"1"
 );

任何想法怎么可以做到这一点?
谢谢.

@H_404_19@ 很简单
$data = array(
   "item1"=>"1","item5"=>"4"
);

$sortingArr = array("5","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
)
原文链接:https://www.f2er.com/php/132079.html

猜你在找的PHP相关文章