Emil Dumbazu
Emil Dumbazu

Reputation: 662

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;  
        }
    }
}

Upvotes: 1

Views: 166

Answers (1)

Rajat Singhal
Rajat Singhal

Reputation: 11264

use usort(), it allowes you to give user defined function to sort..

Documentation..

Upvotes: 1

Related Questions