• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

其实要实现很简单直接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"=>"断桥残雪,三潭映月,君可知为何如此?"
          ]
        ]
      ]
    ]

作  者:。思索
出  处:https://www.cnblogs.com/wangyang0210/p/11278363.html


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php导入Excel进mysql数据库发布时间:2022-07-10
下一篇:
详解PHP输入流php://input发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap