Php array to combine the same key value to generate a new two-dimensional array, two-dimensional array Array

Source: Internet
Author: User

Php array to combine the same key value to generate a new two-dimensional array, two-dimensional array Array

This example describes how to combine the same key value into a new two-dimensional array using a php array. We will share this with you for your reference. The details are as follows:

Source data:

$infos = array(  array(    'a' => 36,    'b' => 'xa',    'c' => '2015-08-28 00:00:00',    'd' => '2015/08/438488a00b3219929282e3652061c2e3.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' => 36,    'b' => 'se',    'c' => '2015-08-28 00:00:00',    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'  ),  array(    'a' => 6,    'b' => 'aw',    'c' => '2015-08-28 00:00:00',    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'  ),  array(    'a' => 36,    'b' => 'bv',    'c' => '2015-08-28 00:00:00',    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'  ),  array(    'a' => 12,    'b' => 'xx',    'c' => '2015-08-27 00:00:00',    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'  ));

Processing requirement: merge the two-dimensional arrays with the same A in the array into an array to generate a new array.

Code:

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

Output:

Array( [36] => Array  (   [0] => Array    (     [a] => 36     [b] => xa     [c] => 2015-08-28 00:00:00     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png    )   [1] => Array    (     [a] => 36     [b] => se     [c] => 2015-08-28 00:00:00     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png    )   [2] => Array    (     [a] => 36     [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] => Array    (     [a] => 6     [b] => aw     [c] => 2015-08-28 00:00:00     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png    )  ) [12] => Array  (   [0] => Array    (     [a] => 12     [b] => xx     [c] => 2015-08-27 00:00:00     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png    )  ))

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.