Merging two-dimensional arrays

Source: Internet
Author: User
Array A before merging:

array (size=3)  0 =>     array (size=2)      'id' => string '113' (length=3)      'email' => string 'yintx_1292342352@163.com' (length=24)      1 =>     array (size=2)      'id' => string '111' (length=3)      'email' => string 'yintx_1293456456@163.com' (length=24)  2 =>     array (size=2)      'id' => string '109' (length=3)      'email' => string 'yintx_129@99999.com' (length=19)

Array B before merging:

array (size=2)  0 =>     array (size=2)      'user_id' => string '113' (length=2)      'count' => string '1' (length=1)  1 =>     array (size=2)      'user_id' => string '109' (length=2)      'count' => string '8' (length=1)

Merged Array (merge condition, id==b of a, user_id when merged ):

array (size=3)  0 =>     array (size=3)      'id' => string '113' (length=3)      'email' => string 'yintx_1292342352@163.com' (length=24)      'count' => string '1' (length=1)      1 =>     array (size=3)      'id' => string '111' (length=3)      'email' => string 'yintx_1293456456@163.com' (length=24)      'count' => string '0' (length=1)  2 =>     array (size=3)      'id' => string '109' (length=3)      'email' => string 'yintx_129@99999.com' (length=19)      'count' => string '8' (length=1)

Reply content:

Array A before merging:

array (size=3)  0 =>     array (size=2)      'id' => string '113' (length=3)      'email' => string 'yintx_1292342352@163.com' (length=24)      1 =>     array (size=2)      'id' => string '111' (length=3)      'email' => string 'yintx_1293456456@163.com' (length=24)  2 =>     array (size=2)      'id' => string '109' (length=3)      'email' => string 'yintx_129@99999.com' (length=19)

Array B before merging:

array (size=2)  0 =>     array (size=2)      'user_id' => string '113' (length=2)      'count' => string '1' (length=1)  1 =>     array (size=2)      'user_id' => string '109' (length=2)      'count' => string '8' (length=1)

Merged Array (merge condition, id==b of a, user_id when merged ):

array (size=3)  0 =>     array (size=3)      'id' => string '113' (length=3)      'email' => string 'yintx_1292342352@163.com' (length=24)      'count' => string '1' (length=1)      1 =>     array (size=3)      'id' => string '111' (length=3)      'email' => string 'yintx_1293456456@163.com' (length=24)      'count' => string '0' (length=1)  2 =>     array (size=3)      'id' => string '109' (length=3)      'email' => string 'yintx_129@99999.com' (length=19)      'count' => string '8' (length=1)

The array_merge_recursive () function, like the Array_merge () function, merges the elements of one or more arrays and appends the values in an array to the previous array. and returns an array as the result.
However, unlike Array_merge (), when there are duplicate key names, the values are not overwritten, but the values of multiple identical key names are recursively grouped into an array. (see Example 1)

$a 1=array ("a" = "Horse", "b" = "Dog");
$a 2=array ("c" = "Cow", "b" = "Cat");
Print_r (array_merge_recursive ($a 1, $a 2));
?>
Output:
Array (
[A] = Horse
[b] = = Array ([0] = Dog [1] = Cat)
[C] = Cow
)

$a = array (' id ' = ' 113 ', ' email ' = ' yintx_1292342352@163.com '), array (' id ' = ' 111 ', ' email ' = ' = ' yintx_ 1293456456@163.com '), array (' id ' = ' 109 ', ' email ' = ' yintx_129@99999.com ')); $b = Array (' user_id ' = ' 113 ', ' count ' = ' 1 '), Array (' user_id ' = ' 109 ', ' count ' = ' 8 ')), foreach ($a as $ka = $va) {foreach ($b as $        Kb=> $vb) {if ($va [' id '] = = $vb [' user_id ']) {$a [$ka] [' count '] = $vb [' count ']; }}}var_dump ($a);----------Array (size=3) 0 = = Array (size=3) ' id ' = = String ' 113 ' (length=3) ' EMA Il ' = = string ' yintx_1292342352@163.com ' (length=24) ' count ' = = String ' 1 ' (length=1) 1 = = Array (size= 2) ' id ' = = String ' 111 ' (length=3) ' email ' = = String ' yintx_1293456456@163.com ' (length=24) 2 = a Rray (size=3) ' id ' = = String ' 109 ' (length=3) ' email ' = = String ' yintx_129@99999.com ' (length=19) ' Co Unt ' = = String ' 8 ' (length=1)

Array_merge_recursive ($a, $b);

  • 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.