Function for adding two arrays in php

Source: Internet
Author: User

Copy codeThe Code is as follows:
<? Php
Function array_add ($ a, $ B ){
// Obtain the intersection of two arrays based on the key name
$ Arr = array_intersect_key ($ a, $ B );
// Traverse the second array. If the key name does not exist with the first array, add the array element to the first array.
Foreach ($ B as $ key => $ value ){
If (! Array_key_exists ($ key, $ )){
$ A [$ key] = $ value;
}
}
// Calculate the sum of array elements with the same key name and replace the element values corresponding to the same key name in the original array
Foreach ($ arr as $ key => $ value ){
$ A [$ key] = $ a [$ key] + $ B [$ key];
}
// Returns the added array.
Return $;
}
$ A = array ('0' => '2', '1' => '4', '3' => '8 ', 'A' => '123 ');
$ B = array ('0' => '5', '2' => '4', 'B' => '33 ', 'A' => '22 ');
$ Arr = array_add ($ a, $ B );
Print_r ($ arr );
?>

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.