PHP combines two arrays and sums them

WBOY
Release: 2023-05-19 15:38:37
Original
783 people have browsed it

In PHP development, arrays are one of the frequently used data types. Array merging is a common operation, but if you need to add and sum elements at the same position in two arrays, how to implement it? This article will introduce two methods to implement the operation of merging and summing two arrays in PHP.

Method 1: Use for loop

The first method is to use for loop to traverse two arrays and use the " " operator to add elements at the same position:

$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(2, 4, 6, 8, 10);

$new_arr = array();
for ($i = 0; $i < count($arr1); $i++) {
  $new_arr[$i] = $arr1[$i] + $arr2[$i];
}

print_r($new_arr);
Copy after login

In the above code, $arr1 and $arr2 are two arrays to be merged, and $new_arr is the merged result array. The for loop traverses the two arrays, adds the elements at the same position, and stores the result in the $new_arr array. Finally, use the print_r function to print the results.

Run the above code, the output result is as follows:

Array
(
    [0] => 3
    [1] => 6
    [2] => 9
    [3] => 12
    [4] => 15
)
Copy after login
Copy after login

Method 2: Use the array_map function

The second method is to use the array_map function to merge and sum the two arrays . The array_map function can pass each element in one or more arrays to a callback function, process it and return a new array.

function sum($a, $b) {
  return $a + $b;
}

$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(2, 4, 6, 8, 10);

$new_arr = array_map('sum', $arr1, $arr2);

print_r($new_arr);
Copy after login

In the above code, we define a function named sum and use the array_map function to apply the sum function to each element in the $arr1 and $arr2 arrays. The array_map function stores the results in the $new_arr array.

Run the above code, the output result is as follows:

Array
(
    [0] => 3
    [1] => 6
    [2] => 9
    [3] => 12
    [4] => 15
)
Copy after login
Copy after login

Comparison of the two methods

The implementation of the above two methods is not difficult, and both can achieve the same effect. However, when dealing with large arrays, using the array_map function is much faster than a for loop. The array_map function is a PHP built-in function and has been optimized so that it can provide higher efficiency when processing large-scale data.

Summary

In PHP development, array merging is a common operation. If you need to add and sum elements at the same position in two arrays, you can use a for loop or the array_map function to achieve this. The former has simple code and is suitable for small amounts of data; the latter is suitable for processing large-scale arrays and has the advantage of high efficiency.

The above is the detailed content of PHP combines two arrays and sums them. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template