douluxia0606 2016-09-26 14:10
浏览 32
已采纳

如何在分区中反转PHP数组

I have a simple array that looks like this:

Array (
    [0] => Array (
        [id] => 8692
        [name] => d
    )
    [1] => Array (
        [id] => 8691
        [name] => c
    )
    [2] => Array (
        [id] => 8690
        [name] => b
    )
    [3] => Array (
        [id] => 8689
        [name] => a
    )
    [4] => Array (
        [id] => 8500
        [name] => d
    )
    [5] => Array (
        [id] => 8499
        [name] => c
    )
    [6] => Array (
        [id] => 8498
        [name] => b
    )
    [7] => Array (
        [id] => 8497
        [name] => a
    )
)

This array is quite long so I only included the first 4 items to give you an idea.

My problem is that I need the array to be in a format of

a,b,c,d,a,b,c,d

At the moment the format is like:

d,c,b,a,d,c,b,a

By this I mean the ['name'] value which is either a,b,c or d.

So I every 4 items in the array need to be reversed.

I have tried to achieve this but fail every time ending up with lots of for & while loops.

  • 写回答

4条回答 默认 最新

  • dongweizhen2009 2016-09-26 14:16
    关注

    You can do it using array_chunk, array_merge and array_reverse:

    $finalArray = array();
    
    $arrays = array_chunk($myArray, 4);
    
    foreach ($arrays as $array) {
        $finalArray = array_merge($finalArray, array_reverse($array));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀