Moving array element to top in PHP

后端 未结 5 1976
陌清茗
陌清茗 2020-12-05 15:27
$arr = array(
    \'a1\'=>\'1\',
    \'a2\'=>\'2\'
);

I need to move the a2 to the top, as well as keep the a2 as a key how woul

5条回答
  •  旧巷少年郎
    2020-12-05 16:13

    You can also look at array_multisort This lets you use one array to sort another. This could, for example, allow you to externalize a hard-coded ordering of values into a config file.

    
    

    In this example, after sorting, the first array will contain 0, 10, 100, 100. The second array will contain 4, 1, 2, 3. The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well.

    Outputs:

    array(4) {
      [0]=> int(0)
      [1]=> int(10)
      [2]=> int(100)
      [3]=> int(100)
    }
    array(4) {
      [0]=> int(4)
      [1]=> int(1)
      [2]=> int(2)
      [3]=> int(3)
    }
    

提交回复
热议问题