PHP Array implementation A method of combining the same key values to generate a new two-dimensional array based on a key value

Source: Internet
Author: User
This article mainly introduces the implementation of PHP array based on a key value to merge the same key values to generate a new two-dimensional array, involving the traversal of the PHP array, assignment-related arithmetic skills, the need for friends can refer to the next

The example in this paper describes a method by which the PHP array implements a new two-dimensional array that merges the same key values according to a key value. Share to everyone for your reference, as follows:

Source data:

$infos = Array (' a ' = = ', ' b ' = = ' xa ', ' c ' = ' 2015-08-28 00:00:00 ', ' d ' = ' = ' 2015/08/4384 88a00b3219929282e3652061c2e3.png '), Array (' a ' = = 3, ' b ' = ' VD ', ' c ' = ' = ' 2015-08-20 00:00:00 ', ' d ' = = ' 2015/08/438488a00b3219929282e3652061c2e3.png '), Array (' a ' = = 6, ' b ' = ' wwe ', ' c ' = ' + ' 2015- 08-28 00:00:00 ', ' d ' = ' 2015/08/438488a00b3219929282e3652061c2e3.png '), Array (' a ' = = ', ' b ' = = ' SE ', ' c ' = ' 2015-08-28 00:00:00 ', ' d ' = ' 2015/08/438488a00b3219929282e3652061c2e3.png '), Array (' a ' =&gt ;  6, ' b ' = ' aw ', ' c ' = ' 2015-08-28 00:00:00 ', ' d ' = ' 2015/08/438488a00b3219929282e3652061c2e3.png '), Array (' a ' = =, ' b ' = ' bv ', ' c ' = ' 2015-08-28 00:00:00 ', ' d ' and ' = ' 2015/08/438488a00b3219929282 E3652061c2e3.png '), Array (' a ' = = ', ' b ' = = ' xx ', ' c ' = ' = ' 2015-08-27 00:00:00 ', ' d ' and ' = ' 2015/0 8/438488a00b3219929282E3652061c2e3.png ')); 

Processing requirements: Merges the same two-dimensional array of a in an array into an array, creating a new array

Code:

<?php $result = Array (); foreach ($infos as $key = + $info) {  $result [$info [' a ']][] = $info;} print_r ($result);

Output:

Array ([+] = = Array ([0] = = Array ([a] = [b] = = XA [c] = = 2015-08-28 00:00:00 [d] = = 2015/08/438488a00b3219929282e3652061c2e3.png) [1] = = Array ([a] = [b] = + se [ c] = 2015-08-28 00:00:00 [d] = 2015/08/438488a00b3219929282e3652061c2e3.png) [2] = = Array ([a    ] = [b] = BV [c] = 2015-08-28 00:00:00 [d] = 2015/08/438488a00b3219929282e3652061c2e3.png )) [3] = = Array ([0] = = Array ([a] = 3 [b] = VD [c] = = 2015-08-20 00:00:00 [d     ] = = 2015/08/438488a00b3219929282e3652061c2e3.png)) [6] = = Array ([0] = = Array ([A] = 6 [b] = WWE [c] = 2015-08-28 00:00:00 [d] = 2015/08/438488a00b3219929282e3652061c2e3.png) [1] =&G T Array ([A] = 6 [b] = AW [c] = 2015-08-28 00:00:00 [d] = 2015/08/438488a00b3219929282e3  652061c2e3.png)) = = Array ([0] = = Array ([a] = [b] = + xx [c] = = 2015-08-27 00:00:00 [d] = > 2015/08/438488a00b3219929282e3652061c2e3.png)))

The above is the whole content of this article, I hope that everyone's study has helped.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.