好的,我已经在stackoverflow中遇到了这个问题,但遗憾的是它在
javascript –
Javascript – 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" );
任何想法怎么可以做到这一点?
谢谢.
很简单
$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 )