PHP数组循环中的数量

时间:2014-12-18 20:31:51

标签: php arrays

我正在尝试使用相同的 car_name和edition_num 来计算数组中元素的数量但不同的买家,我需要将总数量放回到数组中,以便将其保存到数组中数据库中。

我遇到的问题是我选择总和的方式。它通过将匹配元素放入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_sum是我的数组的最后一个索引,但这不起作用:

$inventory[$k-1]['total_buy'] = $buy_sum;

这样做的正确方法是什么? 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>';

        }




?>

期望输出:最后一个数组包含total_buy,它是所有buy_quantites&amp;的总和。 total_sell是包括其自身在内的所有sell_quantities的总和

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
)

1 个答案:

答案 0 :(得分:-1)

你通过创建所有那些额外的循环,变量,数组等来使事情变得如此复杂。没有经过测试,但这应该有效;

$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);
    }
}