如何使最后一个阵列键的数据移动到第一个阵列键?倒数第二个数组键的数据移至第二个数组键

时间:2019-10-17 10:16:52

标签: php arrays sorting multidimensional-array

参考问题的标题,我只是想知道如何更改它? 例如,我的数组数据就是这样

Array(
      [0] => Array(
                  [data] => 'blahblah',
                  [time] => '',
             )
      [1] => Array(
                  [data] => 'abc',
                  [time] => '',
             )
      [2] => Array(
                  [data] => '123',
                  [time] => '',
             )
      [3] => Array(
                  [data] => '456',
                  [time] => '',
             )
      [4] => Array(
                  [data] => '789',
                  [time] => '',
             )
      [5] => Array(
                  [data] => '000',
                  [time] => '',
             )
      [6] => Array(
                  [data] => '111',
                  [time] => '',
             )
)

如何将key [6]数组数据移动到key [0]? key [0]数据将同时移至key [6]。键[5]数据<=>键[1]数据,键[4]数据<=>键[2]数据。提前致谢。编程语言:php

1 个答案:

答案 0 :(得分:2)

您将true作为第二个参数传递,

$arr = array_reverse($arr,true);
print_r($arr);

array_reverse —返回具有相反顺序元素的数组

  

注意:-
   preserve_keys
  如果设置为 TRUE ,则保留数字键。非数字键不受此设置的影响,并且始终会   保存。

Demo

输出:-

Array
(
    [6] => Array
        (
            [data] => 111
            [time] => 
        )

    [5] => Array
        (
            [data] => 000
            [time] => 
        )

    [4] => Array
        (
            [data] => 789
            [time] => 
        )

    [3] => Array
        (
            [data] => 456
            [time] => 
        )

    [2] => Array
        (
            [data] => 123
            [time] => 
        )

    [1] => Array
        (
            [data] => abc
            [time] => 
        )

    [0] => Array
        (
            [data] => blahblah
            [time] => 
        )

)