zoukankan      html  css  js  c++  java
  • php二维数组,按照某个键名重组

    给定一个二维数组,例如 $arr

    $arr = [
                0=>['child_id'=>3,'id'=>1,'addtime'=>'32'],
                1=>['child_id'=>3,'id'=>1,'addtime'=>'54'],
                2=>['child_id'=>4,'id'=>1,'addtime'=>'89'],
                3=>['child_id'=>1,'id'=>1,'addtime'=>'56'],
                4=>['child_id'=>3,'id'=>1,'addtime'=>'34'],
                5=>['child_id'=>4,'id'=>1,'addtime'=>'12'],
                6=>['child_id'=>2,'id'=>1,'addtime'=>'54']
            ];

      $barr = [];
      foreach ($arr as $key => $value) {
        $barr[$value['child_id']][] = $value;
      }
      print_r($barr);
      exit;

      //下面是打印的结果

      [
        3 => [
            0 => ['child_id' => 3,'id' => 1,'addtime' => 32],
            1 => ['child_id' => 3,'id' => 2,'addtime' => 54]
          ],
        4 => [
            0 => ['child_id' => 4,'id' => 3,'addtime' => 89],
            1 => ['child_id' => 4,'id' => 4,'addtime' => 12],
          ],
        1 => [
            0 => ['child_id' => 1,'id' =>5,'addtime' => 56]
          ],

        2 => [
            0 => ['child_id' => 2,'id' =>6,'addtime' => 54]
          ]

      ]

    最后,希望能帮助到大家!

  • 相关阅读:
    meta标签
    Vue(day8)
    Vue(day7)
    Vue(day6)
    Flex布局
    Vue(day5)
    jquery.data()&jquery.extend()
    Promise对象
    Vue(day4)
    Vue(day3)
  • 原文地址:https://www.cnblogs.com/lyzaidxh/p/9970131.html
Copyright © 2011-2022 走看看