phparrayssortingcustom-sort

Move all elements with a specified value to the end of of the array


If I have an array like so (it could be any combination of numbers):

$arr = array(1, 2, 4, 2, 3, 5, 4, 2, 1);

I want to move all the elements that equal 4 to the end of the array, while preserving the order of the other elements, so ideally my resulting array would be:

1, 2, 2, 3, 5, 2, 1, 4, 4

I thought I could achieve this by using a sort function:

uasort($arr, function($a, $b){
    return $b == 4 ? -1 : 1;
});

Which moves the "4" elements to the end, but ruins the order of the other elements, this is my result with the above code:

2, 3, 1, 2, 5, 2, 1, 4, 4

How should my sorting handler function look? / Is there a better way to achieve this than sorting the array?

Note; I want to preserve my array keys (hence uasort())


Solution

  • Here's one way to do it with uasort:

    $arr = array(1, 2, 4, 2, 3, 5, 4, 2, 1);
    
    uasort($arr, function($a, $b){
        if ($a == 4) return -1;
        if ($b == 4) return 1;
        return 0;
    });
    
    $arr = array_reverse($arr, true);
    
    print_r($arr);
    

    The true argument to array_reverse preserves the keys in the array. The return 0 is optional but improves clarity in my opinion.

    Output:

    Array
    (
        [0] => 1
        [3] => 2
        [1] => 2
        [4] => 3
        [5] => 5
        [7] => 2
        [8] => 1
        [2] => 4
        [6] => 4
    )
    

    The fact that some of the keys corresponding to the same value are mixed up is unavoidable.