How can I add all of my array values together in PHP?
If your array consists of numbers, you can use array_sum to calculate a total. Example from the manual:
$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "\n";
If your array consists of strings, you can use implode:
implode(",", $array);
it would turn an array like this:
strawberries
peaches
pears
apples
into a string like this:
strawberries,peaches,pears,apples
Sum all values from a php array
array_sum() will do the job for you.
This function returns sum of all array elements integer or float.
How to sum values from multiple arrays and add to new array with conditions?
Try to sum using the the sector value like this way :
$res = array();
foreach($data as $key=> $value){
$res[$value['SECTOR']] += $value['MCAP'];
}
var_dump($res); // this output the expected result you want
php - how to add all the numbers in an array
If you just need to sum up total
, use sum()
Query Builder method:
$result = DB::table('marks')->where([
['term', $request->term],
['subject', $request->subject],
['class', $student->class],
['arm', $student->arm],
])->sum('total');
Php - Sum up the numbers in an array one by one
The simplest way is just to loop over the values, adding the current array value to the previous output value to create the new output value. You can then convert that into an array of [x => y] values using array_combine
:
$arr=array(1100,3150,4430,4430,5170,7450,7450,7450,8230);
$out = array($arr[0]);
for ($i = 1; $i < count($arr); $i++) {
$out[$i] = $out[$i-1] + $arr[$i];
}
$arr = array_combine($out, $arr);
print_r($arr);
Output:
Array (
[1100] => 1100
[4250] => 3150
[8680] => 4430
[13110] => 4430
[18280] => 5170
[25730] => 7450
[33180] => 7450
[40630] => 7450
[48860] => 8230
)
Demo on 3v4l.org
How to sum all column values in multi-dimensional array?
$sumArray = array();
foreach ($myArray as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}
print_r($sumArray);
php, sum two array values
You can use array_keys
to get the unique from both of the array and then loop through keys to some them
$r = [];
$keys = array_keys($a1+$a2);
foreach($keys as $v){
$r[$v] = (empty($a1[$v]) ? 0 : $a1[$v]) + (empty($a2[$v]) ? 0 : $a2[$v]);
}
Working DEMO
Sum parts of an array in php
The simplest, most efficient way to group and sum is to perform a single loop and assign temporary associative keys.
When a row is identified as a new dlv_id
row, save the two desired elements, otherwise add the no_of_subs
value to the pre-existing value.
Optionally, remove the temporary keys with array_values()
.
Code (Demo)
$array = [
["cust_id" => 1006, "no_of_subs" => 2, "dlv_id" => 1000],
["cust_id" => 1011, "no_of_subs" => 3, "dlv_id" => 1000],
["cust_id" => 1012, "no_of_subs" => 5, "dlv_id" => 1001],
["cust_id" => 1013, "no_of_subs" => 6, "dlv_id" => 1001]
];
foreach ($array as $row) {
if (!isset($result[$row["dlv_id"]])) {
$result[$row["dlv_id"]] = ["dlv_id" => $row["dlv_id"], "no_of_subs" => $row["no_of_subs"]];
} else {
$result[$row["dlv_id"]]["no_of_subs"] += $row["no_of_subs"];
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'dlv_id' => 1000,
'no_of_subs' => 5,
),
1 =>
array (
'dlv_id' => 1001,
'no_of_subs' => 11,
),
)
How merge and sum values by comparing any one or two values of the same array
Because all your nested arrays are with predefined amount of elements, all of those with the same indexes, you can use a for
loop instead of the third foreach
:
$sum = [];
foreach($weekly as $aSet)
{
foreach($aSet as $k => $kv)
{
$sum[$kv[0]][0] = $kv[0];
$sum[$kv[0]][1] = $kv[1];
for($i = 2; $i <= 3; $i++)
{
$sum[$kv[0]][$i] += $kv[$i];
}
}
}
echo "<pre>";
print_r($sum);
echo "</pre>";
With a for
loop you can cycle inside the array considering only the needed elements of that array (in your case the indexes 0
and 1
aren't useful in this loop).
Output:
Array
(
[Week - 1] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 8
[3] => 10
)
[Week - 2] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 12
[3] => 14
)
[Week - 3] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 16
[3] => 18
)
[Week - 4] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
)
If you prefer the array with numbered indexes do that:
echo "<pre>";
print_r(array_values($sum));
echo "</pre>";
Output:
Array
(
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 8
[3] => 10
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 12
[3] => 14
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 16
[3] => 18
)
[3] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
)
Related Topics
Php: Multiple SQL Queries in One MySQL_Query Statement
Least Memory Intensive Way to Read a File in PHP
What Does This Symbol Mean in PHP <=
How to Get the Total Number of Rows of a Group by Query
Laravel App Stopped Working After Upgrading to PHP 8
How to Schedule the Execution of a PHP Script on the Server Side
Smtp Server Response: 530 5.7.0 Must Issue a Starttls Command First
How to Get the Value After the Hash in "Somepage.Php#Name"
Laravel 5 Change Public_Path()
How to Chain Method on a Newly Created Object
Two Simultaneous Ajax Requests Won't Run in Parallel
PHP Add Element to Every Sub Array of Multi Dimension Array
PHP MySQL Update If Exist or Insert If Not
How to Embed Images in HTML Email
How to Detect a Malformed Utf-8 String in PHP