How to change orders of array?

前端 未结 6 2002
悲&欢浪女
悲&欢浪女 2021-02-09 20:04
$a = array(0=>\'a\',1=>\'b\',2=>\'c\', 3=>\'d\');

I want to change the order to be 3,2,0,1

$a = array(3=>\         


        
6条回答
  •  一个人的身影
    2021-02-09 20:35

    function reorder_array(&$array, $new_order) {
      $inverted = array_flip($new_order);
      uksort($array, function($a, $b) use ($inverted) {
        return $inverted[$a] > $inverted[$b];
      });
    }
    
    $a = array(0=>'a',1=>'b',2=>'c', 3=>'d');
    reorder_array($a, array(3, 2, 0, 1));
    
    var_dump($a);
    

    Result:

    Array ( [3] => d [2] => c [0] => a [1] => b )
    

提交回复
热议问题