Home > Backend Development > PHP Tutorial > How to combine two-dimensional arrays with the same field in PHP_php tips

How to combine two-dimensional arrays with the same field in PHP_php tips

PHP中文网
Release: 2023-02-28 09:36:02
Original
3699 people have browsed it

The example in this article describes how PHP combines arrays with the same field in a two-dimensional array. 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) ""
 }
}
Copy after login

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) ""
  }
 }
}
Copy after login

Then. . . 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;
}
Copy after login

. The above is how PHP makes a certain field of the two-dimensional array the same How to combine arrays_php skills content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template