PHP将二维数组某一个字段相同的数组合并起来的方法
发布:smiling 来源: PHP粉丝网 添加日期:2021-07-12 09:55:19 浏览: 评论:0
这篇文章主要介绍了PHP将二维数组某一个字段相同的数组合并起来的方法,涉及PHP多维数组操作的相关技巧,需要的朋友可以参考下。
本文实例讲述了PHP将二维数组某一个字段相同的数组合并起来的方法,分享给大家供大家参考,具体如下:
例子:
- array(3) {
- [0]=>
- array(16) {
- ["id"]=>
- string(2) "42"
- ["uid"]=>
- string(2) "14"
- ["euid"]=>
- string(2) "56"
- ["did"]=>
- string(1) "1"
- ["nid"]=>
- string(1) "0"
- ["phonetime"]=>
- string(10) "1443927600"
- ["createtime"]=>
- string(10) "1443880619"
- ["type"]=>
- string(1) "3"
- ["status"]=>
- string(1) "0"
- ["atype"]=>
- string(1) "1"
- ["mtype"]=>
- string(1) "2"
- ["endtime"]=>
- string(1) "0"
- ["time"]=>
- string(10) "10月04日"
- ["date"]=>
- string(6) "周日"
- ["uname"]=>
- NULL
- ["album"]=>
- string(0) ""
- }
- [1]=>
- array(16) {
- ["id"]=>
- string(2) "40"
- ["uid"]=>
- string(2) "14"
- ["euid"]=>
- string(2) "56"
- ["did"]=>
- string(1) "1"
- ["nid"]=>
- string(1) "0"
- ["phonetime"]=>
- string(10) "1444359600"
- ["createtime"]=>
- string(10) "1444268595"
- ["type"]=>
- string(1) "3"
- ["status"]=>
- string(1) "0"
- ["atype"]=>
- string(1) "1"
- ["mtype"]=>
- string(1) "2"
- ["endtime"]=>
- string(1) "0"
- ["time"]=>
- string(10) "10月09日"
- ["date"]=>
- string(6) "周五"
- ["uname"]=>
- NULL
- ["album"]=>
- string(0) ""
- }
- [2]=>
- array(16) {
- ["id"]=>
- string(2) "43"
- ["uid"]=>
- string(1) "2"
- ["euid"]=>
- string(2) "56"
- ["did"]=>
- string(1) "1"
- ["nid"]=>
- string(1) "0"
- ["phonetime"]=>
- string(10) "1444359620"
- ["createtime"]=>
- string(10) "1444268595"
- ["type"]=>
- string(1) "3"
- ["status"]=>
- string(1) "0"
- ["atype"]=>
- string(1) "1"
- ["mtype"]=>
- string(1) "2"
- ["endtime"]=>
- string(1) "0"
- ["time"]=>
- string(10) "10月09日"
- ["date"]=>
- string(6) "周五"
- ["uname"]=>
- NULL
- ["album"]=>
- string(0) ""
- }
- }
现在想要对这个二维数组下的元素,time相同的合并到同一数组下,想要的效果为:
- array(2) {
- ["10月04日"]=>
- array(1) {
- [0]=>
- array(16) {
- ["id"]=>
- string(2) "42"
- ["uid"]=>
- string(2) "14"
- ["euid"]=>
- string(2) "56"
- ["did"]=>
- string(1) "1"
- ["nid"]=>
- string(1) "0"
- ["phonetime"]=>
- string(10) "1443927600"
- ["createtime"]=>
- string(10) "1443880619"
- ["type"]=>
- string(1) "3"
- ["status"]=>
- string(1) "0"
- ["atype"]=>
- string(1) "1"
- ["mtype"]=>
- string(1) "2"
- ["endtime"]=>
- string(1) "0"
- ["time"]=>
- string(10) "10月04日"
- ["date"]=>
- string(6) "周日"
- ["uname"]=>
- NULL
- ["album"]=>
- string(0) ""
- }
- }
- ["10月09日"]=>
- array(2) {
- [0]=>
- array(16) {
- ["id"]=>
- string(2) "40"
- ["uid"]=>
- string(2) "14"
- ["euid"]=>
- string(2) "56"
- ["did"]=>
- string(1) "1"
- ["nid"]=>
- string(1) "0"
- ["phonetime"]=>
- string(10) "1444359600"
- ["createtime"]=>
- string(10) "1444268595"
- ["type"]=>
- string(1) "3"
- ["status"]=>
- string(1) "0"
- ["atype"]=>
- string(1) "1"
- ["mtype"]=>
- string(1) "2"
- ["endtime"]=>
- string(1) "0"
- ["time"]=>
- string(10) "10月09日"
- ["date"]=>
- string(6) "周五"
- ["uname"]=>
- NULL
- ["album"]=>
- string(0) ""
- }
- [1]=>
- array(16) {
- ["id"]=>
- string(2) "43"
- ["uid"]=>
- string(1) "2"
- ["euid"]=>
- string(2) "56"
- ["did"]=>
- string(1) "1"
- ["nid"]=>
- string(1) "0"
- ["phonetime"]=>
- string(10) "1444359620"
- ["createtime"]=>
- string(10) "1444268595"
- ["type"]=>
- string(1) "3"
- ["status"]=>
- string(1) "0"
- ["atype"]=>
- string(1) "1"
- ["mtype"]=>
- string(1) "2"
- ["endtime"]=>
- string(1) "0"
- ["time"]=>
- string(10) "10月09日"
- ["date"]=>
- string(6) "周五"
- ["uname"]=>
- NULL
- ["album"]=>
- string(0) ""
- }
- }
- }
那么。。。代码很简单,没有想象的那么复杂,想要的结果是个三维数组
$result 为原二维数组
- $res = array(); //想要的结果
- foreach ($result as $k => $v) {
- $res[$v['time']][] = $v;
- }
Tags: PHP二维数组合并
相关文章
- ·php二维数组合并及去重复的方法(2021-05-15)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)