Home > Backend Development > PHP Problem > How to sum identical elements in a 2D array in PHP

How to sum identical elements in a 2D array in PHP

PHPz
Release: 2023-04-25 09:47:34
Original
1261 people have browsed it

In PHP, a two-dimensional array is a very common data structure. It consists of multiple one-dimensional arrays and stores relatively complex data structures. When it comes to the operation of two-dimensional arrays, sometimes it is necessary to sum the same elements. In this article, we will share how to sum identical elements in a two-dimensional array in PHP.

First of all, we need to understand what a two-dimensional array is. A two-dimensional array is an array in which each element is an array. We can define a two-dimensional array in the following way:

$array = array(
  array(1, 2, 3),
  array(4, 5, 6),
  array(7, 8, 9)
);
Copy after login

Here we define a 3x3 two-dimensional array. Among them, each sub-array has the same number of elements and is arranged in a certain order. Next, we'll learn how to sum identical elements in these subarrays.

The first method is to use a foreach loop. We can iterate through each sub-array in the 2D array and use another foreach loop to iterate over each element in the sub-array and finally sum all the same elements. The implementation code is as follows:

$sums = array();

foreach ($array as $subarray) {
  foreach ($subarray as $key => $value) {
    if (isset($sums[$key])) {
      $sums[$key] += $value;
    }
    else {
      $sums[$key] = $value;
    }
  }
}

print_r($sums);
Copy after login

In this code, we first create a new array named $sums. We use a double loop to iterate through each sub-array in the 2D array and each element in each sub-array. For each element, we check if it already exists in $sums. If so, add the original value to the element; if not, add the value to $sums. Finally, we print $sums to see the results.

The second method is to use the array_reduce function. This function takes each element in the array as a parameter and returns a cumulative value. We can use the array merge operator " " to merge all the arrays into one array and use the array_reduce function to sum the elements with the same key. The implementation code is as follows:

$sums = array_reduce($array, function ($result, $subarray) {
  return $result + $subarray;
}, array());

print_r($sums);
Copy after login

In this code, we use the array_reduce function to iterate over each sub-array in the two-dimensional array. During each iteration, we merge all the arrays into one array using the array merge operator " ". Finally, we output the returned results to see the results.

The third method is to use the array_walk_recursive function. This function makes a callback for each element in a multidimensional array. We can use this function to operate on each element in the two-dimensional array. The implementation code is as follows:

$sums = array();

array_walk_recursive($array, function ($value, $key) use (&$sums) {
  if (isset($sums[$key])) {
    $sums[$key] += $value;
  }
  else {
    $sums[$key] = $value;
  }
});

print_r($sums);
Copy after login

In this code, we first create a new array named $sums. We use the array_walk_recursive function to make a callback for each element in the two-dimensional array, such as the value and key of the currently traversed element. In each callback, we check if the current key already exists in $sums and then add its value to the existing value or add it to $sums. Finally, we print $sums to see the results.

To summarize, we can use different methods such as loops, functions, and callback functions to sum the same elements in a two-dimensional array in PHP. It depends on your specific needs and preferences. No matter which method you use, you can easily handle various operations on two-dimensional arrays by judicious use of PHP built-in functions.

The above is the detailed content of How to sum identical elements in a 2D array in PHP. 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