Sort array - that specific values will be first

后端 未结 4 1103
失恋的感觉
失恋的感觉 2020-12-21 08:20

I have a array. for example:

   array(\"Apple\", \"Orange\", \"Banana\", \"Melon\");

i want to sort the array that first will be \"orange\

相关标签:
4条回答
  • 2020-12-21 08:28
    $paths = array_merge(
        array_intersect(["Orange", "Melon"], ["Apple", "Orange", "Banana", "Melon"]),
        array_diff(["Apple", "Orange", "Banana", "Melon"], ["Orange", "Melon"])
    );
    
    0 讨论(0)
  • 2020-12-21 08:37

    Another solution; using a custom function to move an element to the beginning of an array

    function __unshift(&$array, $value){
        $key = array_search($value, $array);
        if($key) unset($array[$key]);
        array_unshift($array, $value);  
        return $array;
    }
    
    $a = array("Apple", "Orange", "Banana", "Melon");
    __unshift($a, "Melon");
    __unshift($a, "Orange");
    print_r($a);
    

    Output:

    Array
    (
        [0] => Orange
        [1] => Melon
        [2] => Apple
        [3] => Banana
    )
    

    Demo

    Or you may use the following to reorder an array using another array having reordered index

    function __reorder(&$a, &$b){
        $c = array();
        foreach($b as $index){
            array_push($c, $a[$index]);
        }
        return $c;
    }
    
    // the original array
    $a = array("Apple", "Orange", "Banana", "Melon");
    // an array with reordered index 
    $b = array(1, 3, 0, 2);
    $c = __reorder($a, $b);
    print_r($c);
    

    Demo

    0 讨论(0)
  • 2020-12-21 08:46

    What you looking for is usort, you can specify custom function to sort the array

    example:

    function cmp($a, $b)
    {
        if ($a == "Orange") {
            return 1;
        }
        if ($b == "Orange") {
            return -1;
        }
    
        return strcmp($a, $b);// or any other sort you want
    }
    
    $arr = array("Apple", "Orange", "Banana", "Melon");
    
    usort($arr, "cmp");
    
    0 讨论(0)
  • 2020-12-21 08:53

    Here's a solution that's longer than the others provided, but more flexible. You can easily expand or change the array of items that you want sorted first.

    $array = array("Apple", "Orange", "Banana", "Melon");
    $sort_first = array("Orange", "Melon");
    
    usort($array, function ($a, $b) use ($sort_first) {
        $order_a = array_search( $a, $sort_first );
        $order_b = array_search( $b, $sort_first );
    
        if ($order_a === false && $order_b !== false) {
            return 1;
        } elseif ($order_b === false && $order_a !== false) {
            return -1;
        } elseif ($order_a === $order_b) {
            return $a <=> $b;
        } else {
            return $order_a <=> $order_b;
        }
    });
    
    // Result: $array = array("Orange", "Melon", "Apple", "Banana");
    

    The function can also be easily altered if the items you're sorting aren't strings, such as arrays or objects. Here's an example that sorts an array of objects:

    // Assuming $array is an array of users with a getName method
    $sort_first = array("Sam", "Chris");
    
    usort($array, function ($a, $b) use ($sort_first) {
        $order_a = array_search( $a->getName(), $sort_first );
        $order_b = array_search( $b->getName(), $sort_first );
    
        if ($order_a === false && $order_b !== false) {
            return 1;
        } elseif ($order_b === false && $order_a !== false) {
            return -1;
        } elseif ($order_a === $order_b) {
            return $a->getName() <=> $b->getName();
        } else {
            return $order_a <=> $order_b;
        }
    });
    
    // $array will now have users Sam and Chris sorted first,
    // and the rest in alphabetical order by name
    
    0 讨论(0)
提交回复
热议问题