PHP
·
发表于 5年以前
·
阅读量:8410
本文实例讲述了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;
}
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。