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

Posted 搬砖小伙子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;

 

 

以上是关于php数组实现根据某个键值将相同键值合并生成新二维数组的方法的主要内容,如果未能解决你的问题,请参考以下文章

php数组根据某键值,把相同键值的合并最终生成一个新的二维数组

php 二维数组中取某个唯一的键值为键名。(通过合并两个数组来创建一个新数组,其中的一个数组元素为键名,另一个数组的元素为键值。)

php 二维数组按某个键值排序

请教各位老师php二维数组 查找相同的值合并 如何写

PHP中如何对二维数组按某个键值进行排序

PHP计算二维数组中一维数组相同键名的,键值相加