How to change orders of array?

前端 未结 6 1983
悲&欢浪女
悲&欢浪女 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:52

    Since arrays in PHP are actually ordered maps, I am unsure if the order of the items is preserved when enumerating.

    If you simply want to enumerate them in a specific order:

    $a = array(0=>'a',1=>'b',2=>'c', 3=>'d');
    $order = array(3, 2, 0, 1);
    
    foreach ($order as $index)
    {
      echo "$index => " . $a[$index] . "\n";
    }
    

提交回复
热议问题