Sort an Array by keys based on another Array?

后端 未结 15 1000
甜味超标
甜味超标 2020-11-22 03:29

Is it possible in PHP to do something like this? How would you go about writing a function? Here is an example. The order is the most important thing.

$custo         


        
15条回答
  •  孤城傲影
    2020-11-22 03:42

    IF you have array in your array, you'll have to adapt the function by Eran a little bit...

    function sortArrayByArray($array,$orderArray) {
        $ordered = array();
        foreach($orderArray as $key => $value) {
            if(array_key_exists($key,$array)) {
                    $ordered[$key] = $array[$key];
                    unset($array[$key]);
            }
        }
        return $ordered + $array;
    }
    

提交回复
热议问题