Viewed   147 times

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 ?

 Answers

1

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

To avoid the looping use array_column() (as of PHP 5.5.0):

array_multisort(array_column($data, 'return_fare'),  SORT_ASC,
                array_column($data, 'one_way_fare'), SORT_ASC,
                $data);
Thursday, November 3, 2022
1
usort($array['javascript']['core'], function($a, $b) {
    return $a['weight'] - $b['weight'];
});
Thursday, December 1, 2022
1
function cmp($a, $b)
{

$sizes = array(
"XXS" => 0,
"XS" => 1,
"S" => 2,
"M" => 3,
"L" => 4,
"XL" => 5,
"XXL" => 6
);

$asize = $sizes[$a];
$bsize = $sizes[$b];

if ($asize == $bsize) {
    return 0;
}

return ($asize > $bsize) ? 1 : -1;
}

usort($your_array, "cmp");
Wednesday, August 24, 2022
 
enrique
 
2

You just need to change your callback function to return negative, zero or positive numbers depending on the values present (or not) in the two items being compared at any one time; just as you would when comparing a single array index in both items.

function callback($a, $b) {
    // Both have [5] so sort by that
    if ($a[5] !== '-' && $b[5] !== '-')
        return $b[5] - $a[5];

    // Neither have [5] so sort by [4]
    if ($a[5] === '-' && $b[5] === '-')
        return $b[4] - $a[4];

    // One has a [5] value
    if ($a[5] === '-')
        return 1;
    else
        return -1;
}
uasort($array, 'callback');
print_r($array);

In this particular case, the above callback could be whittled down to something like:

function callback($a, $b) {
    // Neither have [5] so sort by [4]
    if ($a[5] === '-' && $b[5] === '-')
        return $b[4] - $a[4];

    // One, or both, has a [5] value
    return $b[5] - $a[5];
}
Monday, September 26, 2022
 
3

Use multi-byte string functions. There is a function called strcoll which seems to suit your needs.

More info:

  • On how to sort an array of UTF-8 strings
  • How to sort an array of UTF-8 strings?

EDIT: added Peter's working code, below

setlocale(LC_COLLATE, 'sk_SK.utf8');

usort($fb_friends['data'], 'custom_sort');

function custom_sort($a, $b) {
    return strcoll ($a['last_name'], $b['last_name']);
}

foreach ($fb_friends['data'] as $friend) {
    echo '<br>';
    echo $friend['name'];
}
Wednesday, August 3, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :