Php Data Integration and master challenges-php Tutorial

Source: Internet
Author: User
Php Array merging experts challenge to combine the following arrays into an Array (& nbsp; [0] & gt; Array & nbsp; (& nbsp; [id] & gt; default & nbsp; [name] & gt; aaa & nbsp; [tel] & gt; bbb & nbs php array merging master challenge
Merge the following array into an array
Array
(
[0] => Array
(
[Id] => default
[Name] => aaa
[Tel] => bbb
[Age] => ccc
)

[1] => Array
(
[Id] => 11
[Name] => aaa
[Tel] => bbb
[Age] => ccc
)

[2] => Array
(
[Id] => 22
[Name] => aaa
[Tel] => bbb
[Age] => ccc
)

[3] => Array
(
[Id] => 33
[Name] => aaa
[Tel] => bbb
[Age] => ccc
)

[4] => Array
(
[Id] => 44
[Name] => new
[Tel] => eee
[Age] => fff
)

[5] => Array
(
[Id] => 66
[Name] => new
[Tel] => eee
[Age] => fff
)

)


Merge the preceding array into the following array

Array
(
[0] => Array
(
[Id] => array (
[0] => default
[1] => 11
[2] => 22
[3] => 33
)
[Name] => aaa
[Tel] => bbb
[Age] => ccc
)

[1] => Array
(
[Id] => array (
[0] => 44,
[1] => 66
)
[Name] => new
[Tel] => eee
[Age] => fff
)

)

------ Solution --------------------
PHP code
$ar = array (  0 =>   array (    'id' => 'default',    'name' => 'aaa',    'tel' => 'bbb',    'age' => 'ccc',  ),  1 =>   array (    'id' => '11',    'name' => 'aaa',    'tel' => 'bbb',    'age' => 'ccc',  ),  2 =>   array (    'id' => '22',    'name' => 'aaa',    'tel' => 'bbb',    'age' => 'ccc',  ),  3 =>   array (    'id' => '33',    'name' => 'aaa',    'tel' => 'bbb',    'age' => 'ccc',  ),  4 =>   array (    'id' => '44',    'name' => 'new',    'tel' => 'eee',    'age' => 'fff',  ),  5 =>   array (    'id' => '66',    'name' => 'new',    'tel' => 'eee',    'age' => 'fff',  ),);$res = array();foreach($ar as $r) {  $k = "$r[name]--$r[tel]--$r[age]";  if(! isset($res[$k])) {    $res[$k] = $r;    $res[$k]['id'] = array();  }  $res[$k]['id'][] = $r['id'];}$res = array_values($res);print_r($res);

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.