Merge two 2D arrays

Source: Internet
Author: User
Array a before merging: {code ...} array B before merging: {code ...} merged array (merging condition, when the user_id of idb of a is merged): {code ...} 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)

The merged array (Merging condition, where the id of a is = user_id of B.):

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)

The merged array (Merging condition, where the id of a is = user_id of B.):

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, combines the elements of one or more arrays. The values of an array are appended to the values of the previous array. Returns an array of results.
However, unlike array_merge (), when duplicate key names exist, the value is not overwritten. Instead, multiple values with the same key name are recursively grouped into an array. (See example 1)

$ A1 = array ("a" => "Horse", "B" => "Dog ");
$ A2 = array ("c" => "Cow", "B" => "Cat ");
Print_r (array_merge_recursive ($ a1, $ a2 ));
?>
Output:
Array (
[A] => Horse
[B] => Array ([0] => Dog [1] => Cat)
[C] => Cow
)

$a = array(    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(    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)      'email' => 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 =>     array (size=3)      'id' => string '109' (length=3)      'email' => string 'yintx_129@99999.com' (length=19)      'count' => 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.