PHP——数组根据某一键值合并

前言

其实要实现很简单直接foreach,再根据PHP中数组的特性就可以轻松实现。

步骤

这是源数据的格式

$info = [
    [
        "gname" => "特别关心" ,
        "gid" => 1, "fid" => 2,
        "nickname" => "立冬" ,
        "avatar" => "http://cache.wangyangyang.vip/avatar_4.gif",
        "remark" => "" ,
        "desc" => "雪花一片一片的洒落在窗台上,田野间,为大地换上了新装~"
    ],
    [
        "gname" => "我的好友",
        "gid" => 2,
        "fid" => 3,
        "nickname" => "立春",
        "avatar" => "http://cache.wangyangyang.vip/avatar_3.jpg",
        "remark" => "",
        "desc" => "我在春天等着你的到来~"
    ],

   [
        "gname" => "朋友" ,
        "gid" => 3,
        "fid" => 4,
        "nickname" => "立夏" ,
        "avatar" => "http://cache.wangyangyang.vip/12222222222222.jpg" ,
        "remark" => "" ,
        "desc" => "你好啊,夏天!"
    ],

   [
        "gname" => "同学" ,
        "gid" => 4,
        "fid" => 5,
        "nickname" => "小雨" ,
        "avatar" =>  "http://cache.wangyangyang.vip/avatar_5.jpg" ,
        "remark" => "" ,
        "desc" => "人间四月芳菲尽,山寺桃花始盛开!"
    ],
   [
        "gname" =>"同学" ,
        "gid" => 4,
        "fid" => 6,
        "nickname" => "残雪殇" ,
        "avatar" => "http://cache.wangyangyang.vip/12222222222222.jpg" ,
        "remark" => "小雪" ,
        "desc" => "断桥残雪,三潭映月,君可知为何如此?"
    ]
]

合并后的数据

将数组中某一个相同的值作为键,然后生成一个新的三维数组

 $list = [];
 foreach ($info as $k => $v) {
    $list[$v['gid']]['gid'] = $v['gid'];
    $list[$v['gid']]['gname'] = $v['gname'];
    $list[$v['gid']]['list'][] = $v;
 }              

得到数据

$list=[
      1=>[
        "gid"=>1,
        "gname"=> "特别关心",
        "list"=>[
          [
            "gname"=>"特别关心",
            "gid"=>1,
            "fid"=>2,
            "nickname"=>"立冬",
            "avatar"=>"http://cache.wangyangyang.vip/avatar_4.gif",
            "remark"=>"",
            "desc"=>"雪花一片一片的洒落在窗台上,田野间,为大地换上了新装~"
          ]
        ]
      ],
     2=> [
        "gid"=>2,
        "gname"=> "我的好友",
        "list"=>[
          [
            "gname"=>"我的好友",
            "gid"=>2,
            "fid"=>3,
            "nickname"=> "立春",
            "avatar"=>"http://cache.wangyangyang.vip/avatar_3.jpg",
            "remark"=>"",
            "desc"=> "我在春天等着你的到来~"
          ]
        ]
      ],
      3=>[
        "gid"=>3,
        "gname"=>"朋友",
        "list"=>[
         [
            "gname"=>"朋友",
            "gid"=>3,
            "fid"=>4,
            "nickname"=> "立夏",
            "avatar"=> "http://cache.wangyangyang.vip/12222222222222.jpg",
            "remark"=> "",
            "desc"=>"你好啊,夏天!"
          ]
        ]
      ],
     4=>[
        "gid"=>4,
        "gname"=>"同学",
        "list"=>[
          [
            "gname"=>"同学",
            "gid"=>4,
            "fid"=>5,
            "nickname"=>"小雨",
            "avatar"=>"http://cache.wangyangyang.vip/avatar_5.jpg",
            "remark"=> "",
            "desc"=>"人间四月芳菲尽,山寺桃花始盛开!"
          ],
          [
            "gname"=> "同学",
            "gid"=>4,
            "fid"=>6,
            "nickname"=>"残雪殇",
            "avatar"=>"http://cache.wangyangyang.vip/12222222222222.jpg",
            "remark"=> "小雪",
            "desc"=>"断桥残雪,三潭映月,君可知为何如此?"
          ]
        ]
      ]
    ]
posted @ 2019-08-01 15:48  。思索  阅读(2035)  评论(0编辑  收藏  举报