Monday 3 September 2018

PHP sorting table by table, correspondence key

My first array:

normalArray
(
    [0] => Business Class
    [2] => Economy
    [6] => First Class
)

My sorting array:
sortArray
(
    [0] => Economy
    [1] => Business Class
    [2] => First Class
)

I am trying to get this as my result
resultsArray
(
    [2] => Economy
    [0] => Business Class
    [6] => First Class
)

Note that the key and value needs to follow the correct order. So i would need to sort array by an array while keeping the key to the value.
I have searched around and looked at many different examples.
Thanks

Try this:
$map = array_flip($sortArray);
uasort($normalArray,function($a,$b) use ($map) {return $map[$a] < $map[$b] ? -1 : 1});

0 comments:

Post a Comment