php数组实现根据某个键值将相同键值合并生成新二维数组的方法

源数据

$infos = array(

  array(

    'a' => 36,

    'b' => 'xa',

    'c' => '2015-08-28 00:00:00',

    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'

  ),

  array(

    'a' => 3,

    'b' => 'vd',

    'c' => '2015-08-20 00:00:00',

    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'

  ),

  array(

    'a' => 6,

    'b' => 'wwe',

    'c' => '2015-08-28 00:00:00',

    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'

  ),

  array(

    'a' => 36,

    'b' => 'se',

    'c' => '2015-08-28 00:00:00',

    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'

  ),

  array(

    'a' => 6,

    'b' => 'aw',

    'c' => '2015-08-28 00:00:00',

    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'

  ),

  array(

    'a' => 36,

    'b' => 'bv',

    'c' => '2015-08-28 00:00:00',

    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'

  ),

  array(

    'a' => 12,

    'b' => 'xx',

    'c' => '2015-08-27 00:00:00',

    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'

  )

);
————————————————
版权声明:本文为CSDN博主「tjg888888」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/tianjingang1/article/details/98213158
处理要求:将数组中a相同的二维数组合并到一个数组中,生成一个新的数组

<?php

 $result= array();

 foreach ($infos as $key => $info) {

  $result[$info['a']][] = $info;

 }

 print_r($result);

Array

(

 [36] => Array

  (

   [0] => Array

    (

     [a] => 36

     [b] => xa

     [c] => 2015-08-28 00:00:00

     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png

    )

   [1] => Array

    (

     [a] => 36

     [b] => se

     [c] => 2015-08-28 00:00:00

     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png

    )

   [2] => Array

    (

     [a] => 36

     [b] => bv

     [c] => 2015-08-28 00:00:00

     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png

    )

  )

 [3] => Array

  (

   [0] => Array

    (

     [a] => 3

     [b] => vd

     [c] => 2015-08-20 00:00:00

     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png

    )

  )

 [6] => Array

  (

   [0] => Array

    (

     [a] => 6

     [b] => wwe

     [c] => 2015-08-28 00:00:00

     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png

    )

   [1] => Array

    (

     [a] => 6

     [b] => aw

     [c] => 2015-08-28 00:00:00

     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png

    )

  )

 [12] => Array

  (

   [0] => Array

    (

     [a] => 12

     [b] => xx

     [c] => 2015-08-27 00:00:00

     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png

    )

  )

)

————————————————
版权声明:本文为CSDN博主「tjg888888」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/tianjingang1/article/details/98213158

 

 

---------------------------------------------------------------------------------------------------------------------------------------

$html = '';
        foreach ($result as $k => $v) {
            $html .= "车牌号: ".$k."<br>";

               foreach ($v as $k1 => $v1){
                   $html .= "【".$v1['jl_market_name'].' * '.$v1['pro_id_name'].' * '.$v1['jl_order_num'].' * '.$v1['num_price']."元】" ."<br>";
               }
            $html .= "<br>";
        }

        $html .= "【总数量:".$pro_id_name.' '.$all_product_num."】";
        $html .= "<br>";
        $html .= "【总价格:".$all_product_price."元】";

        return ['code'=>200,'msg'=>'成功!','data'=>$html,'content'=>$arrData['content'],'jl_name'=>$arrData['jl_name'],'product_id'=>$arrData['product_id']];

 

 

--------------------------------将数组中a相同的二维数组合并到一个数组中,生成一个新的数组,再将a子类相同的键再次组装---------------------------------------------------------------------

 

 

 

 

        $do_info = Db::name('delivery_order')
            ->alias('do')
            ->join('delivery_order_detail dod','dod.delivery_order_id=do.id','left')
            ->whereTime('do.create_time', 'today')
            ->field('*')
            ->select();


        $result = [];

        foreach ($do_info as $key => $info) {

            $result[$info['car_id']][] = $info;



        }

       //dump($result);die;

        $result1 = [];
        foreach ($result as $k => $v)
        {

              foreach ($v as $key1 => $value1) {
                  $result1[$k][$value1['market_id']][] = $value1;
               }
        }
        //dump($result1);die;

 

 

posted @ 2020-06-02 18:25  搬砖小伙子  阅读(744)  评论(0编辑  收藏  举报