How to sum values of column in multidimensional array PHP

Created at 25-Nov-2022 , By samar

How to sum values of column in multidimensional array PHP

With this article, we’ll look at some examples of how to address the "How to sum values of column in multidimensional array PHP" problem.

You can sum values of column on php mulidimentional array using array_sum method on array_column method.

<?php
$items = [
    ['label' => 'cake', 'name' => 'Cake', 'price' => 150],
    ['label' => 'pizza', 'name' => 'Pizza', 'price' => 250],
    ['label' => 'puff', 'name' => 'Veg Puff', 'price' => 20],
    ['label' => 'samosa', 'name' => 'Samosa', 'price' => 14]
];
$arrSum = array_sum(array_column($items, 'price'));
print "Sum of Array : ".$arrSum."<br/>";
?>
  • How do you sum a multidimensional array?

    To sum up all the values in a multidimensional array that live under a specific key we make use of two array functions. First, we call the function array_column() , which returns to us an array of all the values found for a specific key. Second, we use the method array_sum() to sum up all those values.

  • How do you sum an array in PHP?

    The array_sum() function returns the sum of all the values in an array(one dimensional and associative). It takes an array parameter and returns the sum of all the values in it. The only argument to the function is the array whose sum needs to be calculated.

  • How to calculate sum of an array in PHP

    You can use the PHP array_sum() function to calculate the sum of all numeric values in an array. As shown in the following example:

    <?php
      $arr1 = array(1, 2, 3);
      $arr2 = array("a" => 1, "b" => 2, "c" => 3);
      echo array_sum($arr1). '<br />';
      echo array_sum($arr2); 
    ?>
    
  • What is PHP array function?

    The array functions allow you to access and manipulate arrays. Simple and multi-dimensional arrays are supported.

  • What is array in PHP syntax?

    An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more.

  • What is array_sum() in multidimentional array?

    Array_Sum In Multidimensional Array Php. There isn’t just one way to solve a problem; rather, there are a number of distinct strategies that can be utilised. In the following examples, we will discuss a variety of different approaches that could be taken.

  • What is array_sum() in PHP?

    The array_sum() function returns the sum of the values in an array. The returned value can be integer or float. It returns 0 if the array is empty.

Back to code snippet queries related php

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.