PHP

Sum All Column Values in Multi-Dimensional Array

In this tutorial, we are going to discuss the methods to sum all column values in multi-dimensional arrays but first, let’s have a quick introduction to arrays.

Arrays

Arrays are used to store multiple data, we can store data permanently in a single array. There are 3 types of arrays.

1)Index array or Numerical array

2)Associative Array

3) Multidimensional-Array

Index arrays

Index arrays are used to store one-dimensional data means it stores data in a linear form, in an index array we can access each value by defining its index value separately.

Example.

array = [html, css, java, php]

Associated arrays

This type of array is used to store two-dimensional data, we store key values in associative arrays whose values we can access by defining their keys. 

Example

array = [a=>html, b=> css, c=>java, d=> php]

Multidimensional-Array 

In the multidimensional array, we can store multiple values, the data can be inserted in more than one dimension like a matrix form.

Example

array ( arr(“HTML”,120),
arr(“HTML”,120),
arr(“PHP”,150),
arr(“PYTHON”,180))

I hope you got the concept of the array and the types of the array now let’s move forward to our main topic check out the examples below.

How to Sum All Column Values in a Multi-Dimensional Array?

Example 1

<?php
$arr = [
    ['company' => 'Toyota', 'model_name' => 'corolla', 'price' => 2500000],
    ['company' => 'Toyota', 'model_name' => 'Revo', 'price' => 7800000],
    ['company' => 'Honda', 'model_name ' => 'Civic', 'price' => 3500000],
    ['comapny' => 'honda', 'model_name' => 'BR-V', 'price' => 1400000]
];

$sum_arr = array_sum(array_column($arr, 'price', 'model_name'));
print "Sum of Array columns: ".$sum_arr."<br/>";
?>

Code Explanation

In the above code we used a multidimensional array with array_ sum() and array_column() functions, first of we declared a multidimensional array then we declared a variable “$sum_arr” we assigned a sum function to this variable, We passed the array column function as a parameter to the array_sum() function and in the array_coulumn() function we passed our multidimensional array, model_name and the prices as a parameter that will return the total prices or sum of all prices of the car models from the multidimensional array, In the end, we just printed the total of all the prices check out the results below.

Output

Sum of Array columns: 15200000

For better understanding have a look at the next example below.

Example 2

<?php
$multi_arr = [

["value1"=>85,"value2"=>14,"value3"=>25,"value4"=>37,"value5"=>89],   
   
["value1"=>11,"value2"=>22,"value3"=>32,"value4"=>45,"value5"=>65],
 ["value1"=>6.2,"value2"=>70.5,"value3"=>8.50,"value4"=>97.10,"value5"=>99.5],   
   
["value1"=>30,"value2"=>50,"value3"=>80,"value4"=>90,"value5"=>100]
];

$add_value1 = array_sum(array_column($multi_arr, "value1"));
echo "total of first column: ".$add_value1."<br/>";

$add_value2 = array_sum(array_column($multi_arr, "value2"));
echo "total of second column: ".$add_value2."<br/>";

$add_value3 = array_sum(array_column($multi_arr, "value3"));
echo "total of third column: ".$add_value3."<br/>";

$add_value4 = array_sum(array_column($multi_arr, "value4"));
echo "total of fourth column: ".$add_value4."<br/>";

$add_value5 = array_sum(array_column($multi_arr, "value5"));
echo "total of fifth column: ".$add_value5."<br/>";
?>

Code Explanation

In the above code we declared a multi-dimensional array, we also used a variable and assigned it the sum_array() function with array_column() function that will return the sum of the first column values, we passed the multidimensional array, and the column value as a parameter.

In the end, we just printed out the output, we followed the same steps for each column to print their sum values check out the results below.

Output

total of first column: 132.2
total of second column: 156.5
total of third column: 145.5
total of fourth column: 269.1
total of fifth column: 353.5

Conclusion

In this tutorial we talked about the methods to sum all column values in multi-dimensional arrays, firstly we saw an introduction to arrays and their types.

We saw sum_array() and array_column() methods with examples, we discussed the code briefly and saw their outputs as well.

That’s all for today hope you like the tutorial if you have any queries regarding this article so feel free to contact us below. Thanks for reading.

Leave a Comment