I am trying to sum quantity over elements in my array with the same car_name and edition_num but different buyers, and I need to put the total quantity back into the array so I can save it to the database.
The problem im having is the way I choose to sum the total. It works by putting matching elements into a quantity
array, unless it hits an element that does not match, then it ->
1. runs a loop over the size of the quantity array and sums the total
2. empties array
3. puts the new element into it.
The problem is in my inventory loop the total_sum
I need to save is into the last index of my array, but this is not working:
$inventory[$k-1]['total_buy'] = $buy_sum;
What is the correct way to do this? http://ideone.com/ZjnuRi
<?php
$len = '[{"car_name":"Mazda","edition_num":"Prius","code":"M",
"buyer":"JamesT","used":0,"buy_price":3.877,"buy_quantity":4,
"sell_price":4.175,"sell_quantity":0},
{"car_name":"Mazda","edition_num":"Prius","code":"M",
"buyer":"steveF","used":0,"buy_price":3.879,"buy_quantity":1,
"sell_price":4.174,"sell_quantity":3},
{"car_name":"Mazda","edition_num":"Prius","code":"M",
"buyer":"KirkL","used":0,"buy_price":3.879,"buy_quantity":4,
"sell_price":4.174,"sell_quantity":0},
{"car_name":"Toyota","edition_num":"Prius","code":"U",
"buyer":"JamesT","used":0,"buy_price":0.007,"buy_quantity":2,
"sell_price":0.042,"sell_quantity":2},
{"car_name":"Toyota","edition_num":"Prius","code":"U",
"buyer":"steveF","used":0,"buy_price":0.007,
"buy_quantity":0,"sell_price":0.042,"sell_quantity":4},{"car_name":"Toyota","edition_num":"Prius","code":"U",
"buyer":"KirkL","used":0,"buy_price":0.007,"buy_quantity":-2,
"sell_price":0.042,"sell_quantity":6}]';
$inventory = json_decode($len,true);
$timeNow = date('Y-m-d H:i:s');
$q_c = array();
for($k=0 ; $k < sizeof($inventory); $k++)
{
$buy_sum = 0;
$sell_sum = 0;
if(empty($q_c))
{
$q_c[] =
array('car_name'=>$inventory[$k]['car_name'],
'edition_num' => $inventory[$k]['edition_num'] ,
'buy_quantity' => $inventory[$k]['buy_quantity'] ,
'sell_quantity' => $inventory[$k]['sell_quantity'] ,
'buyer' => $inventory[$k]['buyer']);
}
// set quantity control to the current car if not empty:
//if quantity control not empty put current car into it.
//same car:
else
{
if($inventory[$k]['car_name'] == $q_c[0]['car_name'] && $inventory[$k]['edition_num'] == $q_c[0]['edition_num'])
{
$q_c[] =
array('car_name'=>$inventory[$k]['car_name'],
'edition_num' => $inventory[$k]['edition_num'] ,
'buy_quantity' => $inventory[$k]['buy_quantity'] ,
'sell_quantity' => $inventory[$k]['sell_quantity'] ,
'buyer' => $inventory[$k]['buyer']
);
}
//new : sum total for last car:
else
{
for($i=0; $i < sizeof($q_c); $i++)
{
$buy_qty = $q_c[$i]['buy_quantity'];
$sell_qty = $q_c[$i]['sell_quantity'];
$buy_sum += intval ($buy_qty);
$sell_sum += intval ($sell_qty);
//----THIS LINE DOES NOT WORK ---
$inventory[$k-1]['total_buy'] = $buy_sum;
//----THIS LINE DOES NOT WORK ---
}
//clear out the array
$q_c = array();
//push current car into it:
$q_c[] =
array('car_name'=>$inventory[$k]['car_name'],
'edition_num' => $inventory[$k]['edition_num'] ,
'buy_quantity' => $inventory[$k]['buy_quantity'] ,
'sell_quantity' => $inventory[$k]['sell_quantity'] ,
'buyer' => $inventory[$k]['buyer']
);
}
}
echo '<pre>';
print_r($inventory[$k]);
echo '</pre>';
}
?>
Array
(
[car_name] => Mazda
[edition_num] => Prius
[code] => M
[buyer] => JamesT
[used] => 0
[buy_price] => 3.877
[buy_quantity] => 4
[sell_price] => 4.175
[sell_quantity] => 0
)
Array
(
[car_name] => Mazda
[edition_num] => Prius
[code] => M
[buyer] => steveF
[used] => 0
[buy_price] => 3.879
[buy_quantity] => 1
[sell_price] => 4.174
[sell_quantity] => 3
)
Array
(
[car_name] => Mazda
[edition_num] => Prius
[code] => M
[buyer] => KirkL
[used] => 0
[buy_price] => 3.879
[buy_quantity] => 4
[sell_price] => 4.174
[sell_quantity] => 0
[total_buy] => 9
[total_sell] => 3
)
You're making things so complicated by creating all those extra loops, variables, arrays etc. Haven't tested but this should work;
$inventory = json_decode($len,true);
$buys = $sells = array();
$cars = array();
foreach($inventory as $index => $item){
$car = $item['car_name'] . $item['edition_num'];
$buys[$car] += $item['buy_quantity'];
$sells[$car] += $item['sell_quantity'];
$numberofCars[$car]++;
$cars[$car][] = $item;
}
foreach($cars as $car_group){
foreach($car_group as $index => $car){
if($index == count($car_group)-1){
$car['total_buy'] = $buys[ $car['car_name'] . $car['edition_num'] ];
$car['total_sell'] = $sells[ $car['car_name'] . $car['edition_num'] ];
}
print_r($car);
}
}