PHP method to combine arrays with the same field in a two-dimensional array, two-dimensional array_PHP tutorial

WBOY
Release: 2016-07-12 08:58:16
Original
1367 people have browsed it

PHP’s method of merging two-dimensional arrays with the same field in a two-dimensional array, two-dimensional array array

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) ""
 }
}

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

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;
}

Copy after login

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.

Articles you may be interested in:

  • Methods for merging and deduplicating two-dimensional arrays in PHP
  • A simple implementation method for sorting two-dimensional arrays in PHP
  • PHP Methods to sort two-dimensional arrays by specified key values
  • Two PHP methods to remove duplicates from two-dimensional arrays
  • PHP methods to remove duplicates from two-dimensional arrays
  • Detailed explanation of two-dimensional array sorting problem in php
  • PHP method to convert one-dimensional array to two-dimensional array
  • PHP array merging and splitting example analysis
  • Relevant merging in php Improvements in merging arrays with the same key value in a certain field
  • Two methods of php array merging
  • Two methods of array merging in PHP and an introduction to their differences

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1104335.htmlTechArticlePHP’s method of merging arrays with the same field in a two-dimensional array. The example of this article describes the two-dimensional array array. PHP method to combine arrays with the same field in two-dimensional arrays. ...
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!