php - 根据第二个给定数组对数组进行排序

时间:2017-03-09 12:40:09

标签: php arrays

$a = array('val1','val2',200, 179,230, 234, 242); 
$b = array(230, 234, 242, 179, 100);

所以数组$ a应该按照数组$ b排序,$ resultArray应该是(&#39; val1&#39;,&#39; val2&#39;,200,230,234,242,179)< / p>

1 个答案:

答案 0 :(得分:0)

到目前为止,这是提供所需数组顺序的唯一答案:

$a = array('val1','val2',200, 179,230, 234, 242); 
$b = array(230, 234, 242, 179, 100);

for($x=0,$size=sizeof($a); $x<$size; ++$x){
    if($key=array_search($a[$x],$b)){
        $result[$size+$key]=$a[$x];
    }else{
        $result[$x]=$a[$x];
    }
}
ksort($result);
$result=array_values($result);
print_r($result);
  

'val1','val2',200,230,234,242,179