phpargumentsarray-multisort

array_multisort() does not modify array in first parameter


I am trying to sort $ar1 by the descending order values of $ar2. Nothing is happening.

$ar1 = array($arperc);
$ar2 = array($arid);
array_multisort($ar1, $ar2);
print_r($ar1);

What am I missing?


Solution

  • If you want to use the elements of $ar2 as sorting keys, you need to change the order of arguments to array_multisort:

    array_multisort($ar2, $ar1);
    

    This will sort $ar2 in ascending order, and also change the order of $ar1 elements exactly as the order of $ar2 is changed by the sorting. To change the order to descending:

    array_multisort($ar2, SORT_DESC, $ar1);