累积关联数组值PHP

时间:2016-11-06 16:35:32

标签: php arrays

我有这段代码:

这是我的数据结构:

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
)

1 个答案:

答案 0 :(得分:0)

我不太了解你的问题,但是

可能是您需要并添加值

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