I want to get the sum of all the values in array in php. Here I have array
$_SESSION['price'][];
I have some values in the array which has been inserted in to array in each iteration. when do var_dump($_SESSION['price']);
of array I am getting
array(1) { [0]=> string(4) "4806" } array(1) { [0]=> string(5) "65000" } array(1) { [0]=> string(5) "44005" } array(1) { [0]=> string(6) "215668" } array(1) { [0]=> string(4) "7896" }
now I want to calculate each value ie 4806
+ 65000
+ 44005
+ 215668
+ 7896
How can I do this?
I tried echo "totalsum".array_sum($_SESSION['cart_total']);
but I got the output
totalsum4806totalsum65000totalsum44005totalsum215668totalsum7896
You can simply use array_sum
like as
echo array_sum(call_user_func_array('array_merge', $arr));
Or for PHP > 5.5.0 You can also use array_column
like as
echo array_sum(array_column($arr,0));
Output:
337375
Apparently you have a 2-dimensional array. Every element of $_SESSION['price']
is an array with one element, rather than a price. I'm not sure why you did it that way, but you'll need to write a loop to access them.
$sum = 0;
foreach ($_SESSION['price'] AS $subarray) {
$sum += $subarray[0];
}
Maybe you should fix whatever is creating the session variable so it makes it a 1-dimensional array. The sub-arrays don't seem to serve any purpose.
I think you use $_SESSION['cart_total']
, but you have to use like $_SESSION['price']
. When the code is like below
$a = array("price" => array("4806", "65000", "44005", "215668", "7896"));
var_dump($a["price"]);
echo "<br>";
echo "totalsum = " . array_sum($a["price"]);
The output will look like below
array(5) { [0]=> string(4) "4806" [1]=> string(5) "65000" [2]=> string(5) "44005" [3]=> string(6) "215668" [4]=> string(4) "7896" }
totalsum = 337375
OR
$a["price"][] = array("4806");
$a["price"][] = array("65000");
$a["price"][] = array("44005");
$a["price"][] = array("215668");
$a["price"][] = array("7896");
$sum = 0;
foreach ($a["price"] AS $price) {
$total += $price[0];
}
echo $total;
Try this: I've manipulated your array
$arr = $_SESSION['price'];
foreach($arr as $key => $val)
{
$newVal[] = $val[0];
}
print_r(array_sum($newVal)); //output is 337375
Normally, you would sum an array like this:
$sum = array_sum($_SESSION['price']);
However, this will not work for you for two reasons:
"
in the var dump). (OK, array_sum
might convert it to an integer for you, so perhaps this is not a problem in practice.) array(1) { [0]=>
in the var dump). If there are no reasons to why you would want to have it like that, the easiest solution would be to just fix those two things when the array is created, so instead of a nested array of strings you have a flat array of integers.
If that is not possible for some reason, you can sum it like this:
$sum = 0;
foreach($_SESSION['price'] as $e) {
// Convert the first element of the sub array to integer and add it to the $sum.
$sum += (int)$e[0];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.