简体   繁体   中英

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..

Documentation..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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