I've an array like this

Array (
 [0] => Array( "destination" => "Sydney",
               "airlines" => "airline_1",
               "one_way_fare" => 100,
               "return_fare => 300
       ),
 [2] => Array( "destination" => "Sydney",
               "airlines" => "airline_2",
               "one_way_fare" => 150,
               "return_fare => 350
       ),
 [3] => Array( "destination" => "Sydney",
               "airlines" => "airline_3",
               "one_way_fare" => 180,
               "return_fare => 380
       )
)

How can i sort the value by return_fare asc , one_way_fare asc ?

I tried array_multisort() but i ended up getting mixed up data..

asort only works for one dimensional array, i need to sort by two values or more, how can i achieve this like in SQL, order by field1 asc,field2 asc ?

share|improve this question

3 Answers

up vote 6 down vote accepted

array_multisort() is the correct function, you must have messed up somehow:

// Obtain a list of columns
foreach ($data as $key => $row) {
    $return_fare[$key]  = $row['return_fare'];
    $one_way_fare[$key] = $row['one_way_fare'];
}

// Sort the data with volume descending, edition ascending
array_multisort($return_fare, SORT_ASC, $one_way_fare, SORT_ASC, $data);

If you take a look at the comments at PHP's manual page for array_multisort(), you can find a very helpful array_orderby() function which allows you to shorten the above to just this:

$sorted = array_orderby($data, 'return_fare', SORT_ASC, 'one_way_fare', SORT_ASC);
share|improve this answer
thanks..! i think i did it exactly the same u advised. can't find the array_orderby() function though on manual page comments – flyclassic Jan 3 '11 at 7:28
@flyclassic, the first comment: php.net/manual/en/function.array-multisort.php#100534 – Tatu Ulmanen Jan 3 '11 at 7:34
thanks.. now i see it.. – flyclassic Jan 3 '11 at 7:45

Ohh, i managed to solve my own question again....

   
function array_multi_sort($array, $on1,$on2, $order=SORT_ASC)
    {

        foreach($array as $key=>$value){
        $one_way_fares[$key] = $value[$on2];
        $return_fares[$key] = $value[$on1];
        }

        array_multisort($return_fares,$order,$one_way_fares,$order,$array);
    }

The thing is i missed out the last parameter $array on array_multisort($return_fares,$order,$one_way_fares,$order,$array); earlier!

share|improve this answer

In addition to array_multisort(), which requires you to build column-arrays first, there is also usort() which doesn't require such a thing.

usort($data, function($a, $b) { 
    $rdiff = $a['return_fare'] - $b['return_fare'];
    if ($rdiff) return $rdiff; 
    return a['one_way_fare'] - $b['one_way_fare']; 
}); // anonymous function requires PHP 5.3 - use "normal" function earlier
share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.