Php 数组循环中的和数
我试图对数组中具有相同车名和版本号但买家不同的元素的数量求和,我需要将总数量放回数组中,以便将其保存到数据库中 我遇到的问题是我选择的合计方式。它的工作原理是将匹配的元素放入Php 数组循环中的和数,php,arrays,Php,Arrays,我试图对数组中具有相同车名和版本号但买家不同的元素的数量求和,我需要将总数量放回数组中,以便将其保存到数据库中 我遇到的问题是我选择的合计方式。它的工作原理是将匹配的元素放入quantity数组,除非它命中不匹配的元素,否则它-> 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. 问题是在我的库存循环中,
quantity
数组,除非它命中不匹配的元素,否则它->
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.
问题是在我的库存循环中,我需要保存的total\u sum
已保存到数组的最后一个索引中,但这不起作用:
$inventory[$k-1]['total\u buy']=$buy\u sum代码>
正确的方法是什么?
通过创建所有那些没有测试过的额外循环、变量、数组等,您使事情变得如此复杂,但这应该可以工作
你能提供一个预期输出的模型吗?@ryanVicent这不是这些问题之一。这是不正确的,因为它没有考虑到我只需要用匹配的car\u name
和edition\u num
求和的部分。代码存在是有原因的。编辑了代码。还是比你的好多了。作为对那个负一的感谢,我不在乎“同一个卖家”规则,所以祝其余的好运。
<?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
)
$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);
}
}