简体   繁体   English

根据另一个数组中的值对数组进行排序

[英]Sort an array based on values from another array

I have an array which i would like to sort it based on values from another one. 我有一个数组,我想根据另一个数组的值对其进行排序。 FIrst array: 第一个数组:

$array1 = ( '2' , [val]->'3' , [val1]->'1')

And second one: 第二个:

 $array2 = (1,4,3)

I've tried a bubble sort but does not work: 我尝试了冒泡排序,但不起作用:

  for ($i = 1 ; $i <= $array1[0] ; $i++){
    for ($j = $i+1 ; $j <= $array1[0] ; $j++){
        if ($array2[$i] < $array2[$j]){
            $temp = $array1[$i];
            $array1[$i] = $array1[$j];
            $array1[$j] = $temp;  
        }
    }
}

use usort() , it allowes you to give user defined function to sort.. 使用usort() ,它使您可以给用户定义的函数排序。

Documentation.. 文档..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM