本文實例講述了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程序設計有所幫助。