fork download
  1. <?php
  2.  
  3. $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}]';
  4.  
  5.  
  6. $inventory = json_decode($len,true);
  7.  
  8. $timeNow = date('Y-m-d H:i:s');
  9. $q_c = array();
  10.  
  11. for($k=0 ; $k < sizeof($inventory); $k++)
  12. {
  13. $buy_sum = 0;
  14. $sell_sum = 0;
  15.  
  16.  
  17.  
  18. if(empty($q_c))
  19. {
  20. $q_c[] =
  21. array('car_name'=>$inventory[$k]['car_name'],
  22. 'edition_num' => $inventory[$k]['edition_num'] ,
  23. 'buy_quantity' => $inventory[$k]['buy_quantity'] ,
  24. 'sell_quantity' => $inventory[$k]['sell_quantity'] ,
  25. 'buyer' => $inventory[$k]['buyer']);
  26. }
  27. // set quantity control to the current car if not empty:
  28.  
  29. //if quantity control not empty put current car into it.
  30.  
  31. //same car:
  32. else
  33. {
  34.  
  35. if($inventory[$k]['car_name'] == $q_c[0]['car_name'] && $inventory[$k]['edition_num'] == $q_c[0]['edition_num'])
  36. {
  37.  
  38. $q_c[] =
  39. array('car_name'=>$inventory[$k]['car_name'],
  40. 'edition_num' => $inventory[$k]['edition_num'] ,
  41. 'buy_quantity' => $inventory[$k]['buy_quantity'] ,
  42. 'sell_quantity' => $inventory[$k]['sell_quantity'] ,
  43. 'buyer' => $inventory[$k]['buyer']
  44. );
  45.  
  46.  
  47. }
  48.  
  49. //new : sum total for last car:
  50. else
  51. {
  52. for($i=0; $i < sizeof($q_c); $i++)
  53. {
  54.  
  55. $buy_qty = $q_c[$i]['buy_quantity'];
  56. $sell_qty = $q_c[$i]['sell_quantity'];
  57.  
  58. $buy_sum += intval ($buy_qty);
  59. $sell_sum += intval ($sell_qty);
  60.  
  61. //----THIS LINE DOES NOT WORK ---
  62. $inventory[$k-1]['total_buy'] = $buy_sum;
  63. //----THIS LINE DOES NOT WORK ---
  64.  
  65. }
  66. //clear out the array
  67. $q_c = array();
  68.  
  69. //push current card into it:
  70. $q_c[] =
  71. array('car_name'=>$inventory[$k]['car_name'],
  72. 'edition_num' => $inventory[$k]['edition_num'] ,
  73. 'buy_quantity' => $inventory[$k]['buy_quantity'] ,
  74. 'sell_quantity' => $inventory[$k]['sell_quantity'] ,
  75. 'buyer' => $inventory[$k]['buyer']
  76. );
  77.  
  78.  
  79.  
  80. }
  81.  
  82. }
  83.  
  84.  
  85.  
  86. //sum up the inventory count for the same card in the same set //
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. print_r($inventory[$k]);
  94.  
  95.  
  96. }
  97.  
  98.  
  99.  
  100.  
  101. ?>
  102.  
Success #stdin #stdout 0.02s 20568KB
stdin
Standard input is empty
stdout
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
)
Array
(
    [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
)
Array
(
    [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
)
Array
(
    [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
)