Home > Article > Backend Development > PHP method to combine arrays with the same field in a two-dimensional array, two-dimensional array_PHP tutorial
This article describes an example of how PHP combines arrays with the same field in a two-dimensional array method of merging. Share it with everyone for your reference, the details are as follows:
Example:
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) "" } }
Now I want to merge the elements in this two-dimensional array into the same array with the same time. The desired effect is:
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) "" } } }
So. . . The code is very simple, not as complicated as imagined. The desired result is a three-dimensional array
$result is the original two-dimensional array
$res = array(); //想要的结果 foreach ($result as $k => $v) { $res[$v['time']][] = $v; }
Readers who are interested in more PHP related content can check out the special topics of this site: "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php mysql database operation introductory tutorial" Summary of common database operation skills in PHP》
I hope this article will be helpful to everyone in PHP programming.