how to calculate sum of total key in same array - php

i need to calculate the total key within the following array that contains 4 different sub arrays
Array
(
[0] => Array
(
[total] => 4.2
[sku] => 4321
)
[1] => Array
(
[total] => 2
[sku] => 2456
)
[2] => Array
(
[total] => 3
[sku] => 2245
)
[3] => Array
(
[total] => 1.5
[sku] => 2674
)
)
i was calculating it using mysql directly but i prefer to use php
$sql = "SELECT SUM(CAST(IFNULL(total,0) AS DECIMAL(10,2))) FROM trans WHERE trans.userid = :userid";
so total has to be in the same format as from the query
10.70

You can loop over the array, adding up as you go, and then formatting with number_format(), or use array_column() to extract the 'total', and use array_sum() to add them, and again, using number_format() or sprintf() to format them.
echo number_format(array_sum(array_column($array, 'total')), 2);

I guess something like this:-
$array = Array
(
[0] => Array
(
[total] => 4.2
[sku] => 4321
)
[1] => Array
(
[total] => 2
[sku] => 2456
)
[2] => Array
(
[total] => 3
[sku] => 2245
)
[3] => Array
(
[total] => 1.5
[sku] => 2674
)
);
$total = 0;
foreach ($array as $key => $value) {
$total += $value['total'];
}
print_r($total);

you can iterate your array and sum all values like:
<?php
$array = array(
array(
'total' => 4.2,
'sku' => 4321
),
array(
'total' => 2,
'sku' => 2456
),
array(
'total' => 3,
'sku' => 2245
),
array(
'total' => 1.5,
'sku' => 2674
),
);
$result = array('total' => 0,'sku' => 0);
foreach($array as $key => $value) {
$result['total'] += $value['total'];
$result['sku'] += $value['sku'];
}
var_dump($result);
Result: array(2) { ["total"]=> float(10.7) ["sku"]=> int(11696) }
for single total sum output:
echo $result['total'];

You can also try with array_walk_recursive() like this https://eval.in/875555
$input = [your_2d_array_goes_here];
$final = array();
array_walk_recursive($input, function($item, $key) use (&$final){
$final[$key] = isset($final[$key]) ? $item + $final[$key] : $item;
});
print '<pre>';
print_r($final);
print '</pre>';
print $final['total'];
?>

Related

How to concatenate two array in php

How to concatenate two arrays to a single array? In date position 0 and 1 are both concatenated in loop, my array code below here.
Array
(
[Apr-2019] => Array
(
[0] => Array
(
[DateUser] => Apr-2019
[withdarw_amount] => 4.00
)
[1] => Array
(
[current_deposit_amount] => 1.00
[current_deposit_table_refer] => 0.00
[current_deposit_user_refer] => 0.10
[DateUser] => Apr-2019
)
)
)
like my output:
[Apr-2019] => Array
(
[DateUser] => Apr-2019
[withdarw_amount] => 4.00
[current_deposit_amount] => 1.00
[current_deposit_table_refer] => 0.00
[current_deposit_user_refer] => 0.10
[DateUser] => Apr-2019
)
I have tried to use this code,
$data = array_merge($withdrow_amount,$data_casback,$cashbonus_data,$data_discount,$CurrentDeposit);
$months = array();
foreach($data as $date) {
$month = substr($date['DateUser'], 0, 8);
$months[$month][] = $date;
}
echo '<pre>'; print_r($months); die;
You can iterate over your array, using array_merge with the splat operator ... to flatten the internal arrays. Note you can't have two DateUser keys in an array so one will be deleted; assuming they have the same values as in your data that will not be a problem:
$array = array (
'Apr-2019' =>
array (
0 =>
array (
'DateUser' => 'Apr-2019',
'withdarw_amount' => 4.00
),
1 =>
array (
'current_deposit_amount' => 1.00,
'current_deposit_table_refer' => 0.00,
'current_deposit_user_refer' => 0.10,
'DateUser' => 'Apr-2019'
),
),
'Jun-2019' =>
array (
0 =>
array (
'DateUser' => 'Jun-2019',
'withdarw_amount' => 334.00
),
)
);
foreach ($array as &$arr) {
$arr = array_merge(...$arr);
}
print_r($array);
Output:
Array
(
[Apr-2019] => Array
(
[DateUser] => Apr-2019
[withdarw_amount] => 4
[current_deposit_amount] => 1
[current_deposit_table_refer] => 0
[current_deposit_user_refer] => 0.1
)
[Jun-2019] => Array
(
[DateUser] => Jun-2019
[withdarw_amount] => 334
)
)
Demo on 3v4l.org
You can use simple loops also to do that -
$new = [];
foreach ($array as $key =>$a) {
$new[$key] = []; // Define with key
foreach ($a as $v) {
$new[$key] += $v; // Concat
}
}

How to merge array and sum up the values depending on key?

I have an array something like below. I want to merge the nested array and display the totals. Here ID = 60.I want to merge this [0] and [1] depending in the ID value i.e., 60.
Array(
[0] => Array
(
[ID] => 60
[TOTAL] => 500
)
[1] => Array
(
[ID] => 60
[TOTAL] => 600
)
[2] => Array
(
[ID] => 61
[TOTAL] => 600
)
)
I tried with two for loops
foreach($result as $key=>$value){
foreach($result as $key1 => $value1){
// Do stuffs here
}
}
I want the output as
Array(
[0] => Array(
[ID] =>60
[TOTAL] => 1100
)
[1] =>Array(
[ID] =>61
[TOTAL] => 600
)
)
<?php
$result = Array(
Array
(
'ID' => 60,
'TOTAL' => 500
),
Array
(
'ID' => 60,
'TOTAL' => 600
),
Array
(
'ID' => 61,
'TOTAL' => 600
)
);
$set = [];
foreach($result as $data){
if(!isset($set[$data['ID']])) $set[$data['ID']] = 0;
$set[$data['ID']] += $data['TOTAL'];
}
$result_set = [];
foreach($set as $id => $total){
$result_set[] = [
'ID' => $id,
'TOTAL' => $total
];
}
print_r($result_set);
Demo: https://3v4l.org/NMmHC
We store IDs as keys in our $set array and keep adding the total to it whenever we come across the same key in the foreach loop.
In the end, we collect the results in a new array with ID and it's respective total.
My suggestion is to use array_reduce() which iteratively reduce the array to a single value using a callback function.
$arr = [['ID' => 60,'TOTAL' => 500],['ID' => 60,'TOTAL' => 600],['ID' => 61,'TOTAL' => 600]];
$arr = array_reduce($arr, function($acc, $new) {
if (!isset($acc[$new['ID']])) {
$acc[$new['ID']] = $new;
return $acc;
}
$acc[$new['ID']]['TOTAL'] += $new['TOTAL'];
return $acc;
}, []);
echo '<pre>', print_r(array_values($arr));
Working demo.
Solution 1
$arrayvariable=Array(
[0] => Array
(
[ID] => 60
[TOTAL] => 500
)
[1] => Array
(
[ID] => 60
[TOTAL] => 600
)
[2] => Array
(
[ID] => 61
[TOTAL] => 600
)
)
$output = array_reduce($arrayvariable, function (array $compare, array $item) {
$intrestkey = $item ['ID'];
if (array_key_exists($intrestkey, $compare)) {
$compare [$intrestkey] ['TOTAL'] += $item ['TOTAL'];
} else {
$compare [$intrestkey] = $item;
}
return $compare;
}, array());
$incremetedmerged_array = array_values($output);
print_r($incremetedmerged_array); //Produces
Array(
[0] => Array(
[ID] =>60
[TOTAL] => 1100
)
[1] =>Array(
[ID] =>61
[TOTAL] => 600
)
)

Group subsets of data in 3-level array by identifying column

I've this type of array in PHP:
Array(
[100] => Array(
[1] => Array (
[AVA_Date] => 2019-04-18
[ROO_Id] => 100
[RAT_Id] => 9
)
[2] => Array (
[AVA_Date] => 2019-04-20
[ROO_Id] => 100
[RAT_Id] => 10
)
[4] => Array (
[AVA_Date] => 2019-04-21
[ROO_Id] => 100
[RAT_Id] => 10
)
[7] => Array (
[AVA_Date] => 2019-04-22
[ROO_Id] => 100
[RAT_Id] => 9
)
)
)
I would like to merge items on ROO_Id and RAT_Id.
Then, for the AVA_Date, I need to list them under a new array in the current array.
So, the desired output is:
Array(
[100] => Array(
[0] => Array (
[AVA_Date] => Array (
[0] => 2019-04-18
[1] => 2019-04-22
)
[ROO_Id] => 100
[RAT_Id] => 9
)
[1] => Array (
[AVA_Date] => Array (
[0] => 2019-04-20
[1] => 2019-04-21
)
[ROO_Id] => 100
[RAT_Id] => 10
)
)
)
Here what I have tried:
$newArrOtherRooms = array_reduce($newArr, function($acc, $val) {
$room = array_search($val['ROO_Id'], array_column($acc, 'ROO_Id'));
$rate = array_search($val['RAT_Id'], array_column($acc, 'RAT_Id'));
if($rate == $room && $room > -1) {
array_push($acc[$room]['AVA_Date'], $val['AVA_Date']);
}
else {
$new_arr = $val;
$new_arr['AVA_Date'] = [$val['AVA_Date']];
array_push($acc, $new_arr);
}
return $acc;
},[]);
But it doesn't work like I want.
There are a couple of issues with your code. Firstly, you need to wrap the array_reduce with a foreach over the outer level of $newArr. Secondly, your call to array_search doesn't consider the fact that a ROO_Id or RAT_Id value might exist more than once in the array, as it only returns the first key at which it finds the value. To work around this, you can use array_keys to get an array of key values for each ROO_Id and RAT_Id value, and then take the intersection of those two arrays using array_intersect to see if both are present in the same element. If so, you update that element, otherwise you create a new one:
foreach ($newArr as $key => $array) {
$newArrOtherRooms[$key] = array_reduce($array, function($acc, $val) {
$room = array_keys(array_column($acc, 'ROO_Id'), $val['ROO_Id']);
$rate = array_keys(array_column($acc, 'RAT_Id'), $val['RAT_Id']);
$common = array_intersect($room, $rate);
if(!empty($common)) {
array_push($acc[current($common)]['AVA_Date'], $val['AVA_Date']);
}
else {
$new_arr = $val;
$new_arr['AVA_Date'] = [$val['AVA_Date']];
array_push($acc, $new_arr);
}
return $acc;
},[]);
}
print_r($newArrOtherRooms);
Output:
Array(
[100] => Array(
[0] => Array (
[AVA_Date] => Array (
[0] => 2019-04-18
[1] => 2019-04-22
)
[ROO_Id] => 100
[RAT_Id] => 9
)
[1] => Array (
[AVA_Date] => Array (
[0] => 2019-04-20
[1] => 2019-04-21
)
[ROO_Id] => 100
[RAT_Id] => 10
)
)
)
Demo on 3v4l.org
There is absolutely no reason to be making all of those iterated function calls.
Use a nested loop to iterate the subset of data for each room, group on the room "rate id", and push all "available date" values into a subarray in the respective group. When the subset of data is fully iterated, push its grouped data into the result array.
Code: (Demo)
$result = [];
foreach ($newArr as $rooId => $rows) {
$groups = [];
foreach ($rows as $row) {
if (!isset($groups[$row['RAT_Id']])) {
$row['AVA_Date'] = (array) $row['AVA_Date'];
$groups[$row['RAT_Id']] = $row;
} else {
$groups[$row['RAT_Id']]['AVA_Date'][] = $row['AVA_Date'];
}
}
$result[$rooId] = array_values($groups);
}
var_export($result);
Output:
array (
100 =>
array (
0 =>
array (
'AVA_Date' =>
array (
0 => '2019-04-18',
1 => '2019-04-22',
),
'ROO_Id' => 100,
'RAT_Id' => 9,
),
1 =>
array (
'AVA_Date' =>
array (
0 => '2019-04-20',
1 => '2019-04-21',
),
'ROO_Id' => 100,
'RAT_Id' => 10,
),
),
)

Matching array value (PHP Array)

Input post :
$_POST['dateSlot']
$_POST['timeStart']
$_POST['timeEnd']
$_POST['quota']
These input post will resulting the below array.
Array
(
[dateSlot] => Array
(
[0] => 2018-04-05
[1] => 2018-04-05
[2] => 2018-04-05
)
[timeStart] => Array
(
[0] => 11:06 AM
[1] => 10:06 AM
[2] => 9:06 AM
)
[timeEnd] => Array
(
[0] => 11:06 AM
[1] => 9:06 AM
[2] => 7:06 AM
)
[quota] => Array
(
[0] => 12
[1] => 10
[2] => 10
)
)
I'm trying to foreach them to match the index key and form another array with this idea. Not so sure if can get the value I want :
foreach ($_POST['dateSlot'] as $k => $val) {
foreach ($_POST['timeStart'] as $k2 => $val2) {
foreach ($_POST['timeEnd'] as $k3 => $val3) {
foreach ($_POST['quota'] as $k4 => $val4) {
if($k == $k2 && $k == $k3 && $k == $k4){
$timeslots[$k]['date_slot'] = $val;
$timeslots[$k]['time_start'] = $val2;
$timeslots[$k]['time_end'] = $val3;
$timeslots[$k]['event_quota'] = $val4;
}
}
}
}
}
By that foreach, I'm getting the error Illegal string offset for date_slot, time_start, time_end, and event_quota
Based on the rows in the array, my goal is to re-form the array so that they all will be combined together to form 3 rows.
Example :
Array
(
[0] => Array
(
[date_slot] => 2018-04-05
[time_start] => 11:06 AM
[time_end] => 11:06 AM
[event_quota] => 12
)
[1] => Array
(
[date_slot] => 2018-04-05
[time_start] => 10:06 AM
[time_end] => 9:06 AM
[event_quota] => 10
)
[2] => Array
(
[date_slot] => 2018-04-05
[time_start] => 9:06 AM
[time_end] => 7:06 AM
[event_quota] => 10
)
)
Another approach to grouping this kind of data without needing to know the key names in advance.
This works by using the first row's data current( $data ) as the main iterator, then builds an array by combining the outer keys array_keys( $data ) and the inner column value array_column( $data, $column ) with array_combine() which combines two arrays of keys and an array of value to make each row's final array structure keyed by column name.
This is absolutely reliant on each multidimensional array having the same count of elements. As such this is not suitable for forms with checkbox inputs in them. At which point I would suggest using name="row[0][ColumnName]" as your name attribute and negating the need for this array processing.
http://php.net/manual/en/function.array-column.php
http://php.net/manual/en/function.array-combine.php
http://php.net/manual/en/function.array-keys.php
$data = array(
'Column-1'=>array('Row-1a','Row-2a','Row-3a'),
'Column-2'=>array('Row-1b','Row-2b','Row-3b'),
'Column-3'=>array('Row-1c','Row-2c','Row-3c')
);
$array = array();
foreach( array_keys( current( $data ) ) as $column )
{
$array[] = array_combine( array_keys( $data ), array_column( $data, $column ) );
}
print_r( $array );
Produces
Array
(
[0] => Array
(
[Column-1] => Row-1a
[Column-2] => Row-1b
[Column-3] => Row-1c
)
[1] => Array
(
[Column-1] => Row-2a
[Column-2] => Row-2b
[Column-3] => Row-2c
)
[2] => Array
(
[Column-1] => Row-3a
[Column-2] => Row-3b
[Column-3] => Row-3c
)
)
If you know that the element keys in all 4 of those post variables will always correlate to one timeslot element, then I think this will work for you:
foreach ($_POST['dateSlot'] as $key => $value) {
$timeslots[$key] = [
'date_slot' => $_POST['dateSlot'][$key],
'time_start' => $_POST['timeStart'][$key],
'time_end' => $_POST['timeEnd'][$key],
'event_quota' => $_POST['quota'][$key],
];
}
print_r($timeslots);
$dateSlot = $_POST['dateSlot']
$timeStart = $_POST['timeStart']
$timeEnd = $_POST['timeEnd']
$quota = $_POST['quota']
$all = array();
foreach($dateSlot as $key => $date) {
$all[] = array(
"data_slot" => $dateSlot[$key],
"time_start" => $timeStart[$key],
"time_end" => $timeEnd[$key],
"quota" => $quota[$key]
)
}
Input
$array = array(
'dateSlot' => array('2018-04-05','2018-04-05','2018-04-05'),
'timeStart' => array('11:06 AM','10:06 AM','9:06 AM'),
'timeEnd' => array('11:06 AM','9:06 AM','7:06 AM'),
'quota' => array(12,10,10)
);
Solution
$new = array();
for($i=0;$i<count($array['dateSlot']);$i++){
$new[] = array(
'dateSlot' => $array['dateSlot'][$i],
'timeStart' => $array['timeStart'][$i],
'timeEnd' => $array['timeEnd'][$i],
'event_quota' => $array['quota'][$i],
);
}
echo "<pre>";print_r($new);
Output
Array
(
[0] => Array
(
[dateSlot] => 2018-04-05
[timeStart] => 11:06 AM
[timeEnd] => 11:06 AM
[event_quota] => 12
)
[1] => Array
(
[dateSlot] => 2018-04-05
[timeStart] => 10:06 AM
[timeEnd] => 9:06 AM
[event_quota] => 10
)
[2] => Array
(
[dateSlot] => 2018-04-05
[timeStart] => 9:06 AM
[timeEnd] => 7:06 AM
[event_quota] => 10
)
)

how to group a multidimensional array using a condition in php?

I have the following array
Array
(
[0] => Array
(
[shop] => 3
[price] => 332.00
)
[1] => Array
(
[shop] => 1
[price] => 3335.00
)
[2] => Array
(
[shop] => 3
[price] => 235.00
)
[3] => Array
(
[shop] => 1
[price] => 402.50
)
[4] => Array
(
[shop] => 3
[price] => 332.00
)
)
I need to group using shop and get get the minimum price of each shop in the array.
The expected result is as follows
Array
(
[0] => Array
(
[shop] => 3
[price] => 235.00
)
[1] => Array
(
[shop] => 1
[price] => 402.50
)
)
How will I do it?
You need to use additional variable
<?php
$arr = Array
(
0 => Array
(
'shop' => 3,
'price' => 332.00
),
1 => Array
(
'shop' => 3,
'price' => 232.00
),
2 => Array
(
'shop' => 1,
'price' => 232.00
),
3 => Array
(
'shop' => 3,
'price' => 432.00
),
4 => Array
(
'shop' => 1,
'price' => 132.00
),
);
$filtered = array();
foreach($arr as $prices){
if(FALSE === isset($filtered[$prices['shop']]) || $filtered[$prices['shop']]['price'] > $prices['price']){
$filtered[$prices['shop']] = $prices;
}
}
$filtered = array_values($filtered);
print_r($filtered);
This is very fast example how you can achieve this
It's rather simple.
Create a new array where you will host your stores as keys, and prices as values. What you want to do is to go through each element and first if the key does not exists in your new array, add it and its value. If however the key already exists, check if the current value is lower, and save it if true.
$grouped = [];
foreach ($arr as $k => $v) {
foreach ($k as $key => $value) {
if (isset($grouped[$key])) {
if ($value < $grouped[$key]) {
$grouped[$key] = $value;
}
} else {
$grouped[$key] = $value;
}
}
}
Your new array will look like this (store => price):
[
1 => 402.50,
3 => 235.00
]

Categories