Sort an Array by keys based on another Array?

后端 未结 15 789
甜味超标
甜味超标 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 04:00

    Without magic...

    $array=array(28=>c,4=>b,5=>a);
    $seq=array(5,4,28);    
    SortByKeyList($array,$seq) result: array(5=>a,4=>b,28=>c);
    
    function sortByKeyList($array,$seq){
        $ret=array();
        if(empty($array) || empty($seq)) return false;
        foreach($seq as $key){$ret[$key]=$dataset[$key];}
        return $ret;
    }
    

提交回复
热议问题