2016-11-06 97 views
0

我有這樣的代碼:累積關聯數組值PHP

這是我的數據結構:

enter image description here

totalMes功能:

function totalMes(){ 
    //Genera un array con el total de cada mes 
    global $tickets; 
    $totales = ["juego" => '', "precioTicket" => 0, "cantidadTickets" => 0]; 
    for ($i=0; $i < 12; $i++) { 
     foreach($tickets[$i] as $juegos){ 
      foreach ($juegos as $key => $value){ 
       $totales[$key] += $value; 
       $totalMes[$i] = $totales['precioTicket'] * $totales['cantidadTickets']; 
      } 
      $totales = ["juego" => '', "precioTicket" => 0, "cantidadTickets" => 0]; 
     } 
    } 
    return $totalMes; 
} 

現在,當我進入新的價值陣列門票,然後我打電話totalMes函數。它僅累積陣列票券的最後一個值。

例子:

function ingresarJuego($mes, $juego, $precioTicket, $cantidadTickets){ 
    global $tickets; 
    $juego = ["juego" => $juego, "precioTicket" => $precioTicket, "cantidadTickets" => $cantidadTickets]; 
    array_push($tickets[$mes], $juego); 
    return $tickets; 
} 

這是我的數組totalMes,進入一個新的記錄數組票前:

Array 
(
    [0] => 100 
    [1] => 1886 
    [2] => 774 
    [3] => 720 
    [4] => 7719 
    [5] => 5238 
    [6] => 2430 
    [7] => 2736 
    [8] => 1080 
    [9] => 315 
    [10] => 621 
    [11] => 6536 
) 

現在,如果我在陣列票輸入一個值,然後計算totalMes再次。它不積累最後一條記錄時的值。

Array 
(
    [0] => 4 // When it should be 100 + 4 
    [1] => 1886 
    [2] => 774 
    [3] => 720 
    [4] => 7719 
    [5] => 5238 
    [6] => 2430 
    [7] => 2736 
    [8] => 1080 
    [9] => 315 
    [10] => 621 
    [11] => 6536 
) 

回答

0

我不明白你的好,但questio

可能是你需要的,並添加了價值

array_push($tickets[$mes],$tickets[$mes] + $juego);