PHP _php tutorial for adding 2 arrays to a function

Source: Internet
Author: User
Copy CodeThe code is as follows:
function Array_add ($a, $b) {
Gets the intersection of two arrays based on the key name
$arr =array_intersect_key ($a, $b);
Iterates through the second array if the key name does not exist with the first array, adding the array element to the first array
foreach ($b as $key = = $value) {
if (!array_key_exists ($key, $a)) {
$a [$key]= $value;
}
}
Computes the and of the same array element with the same key name, and replaces the element value 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;
}
$a = Array (' 0 ' = ' 2 ', ' 1 ' = ' 4 ', ' 3 ' = ' 8 ', ' a ' = ' 100 ');
$b = Array (' 0 ' = ' 5 ', ' 2 ' = ' 4 ', ' b ' = ' + ', ' a ' = ' 22 ');
$arr =array_add ($a, $b);
Print_r ($arr);
?>

http://www.bkjia.com/PHPjc/323646.html www.bkjia.com true http://www.bkjia.com/PHPjc/323646.html techarticle Copy the code as follows: Php function Array_add ($a, $b) {//Gets 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 ...

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