How can I add an array inside an array?

2

I need a multidimensional array that will be composed of information from the database, but I'm not able to add this information. I can not explain very well, so I'll show you.

As soon as I want it:

Array
(
    [0] => Array
        (
            [valor_apostado] => 100
            [valor_final] => 817
            [id_aposta] => 1022
            [0] => Array
                (
                    [timeApostado] => Motherwell
                        [partidaApostada] => Motherwell x Hearts
                    )
                [1] => Array
                    (
                        [timeApostado] => Hearts
                        [partidaApostada] => Pandora x Hearts
                    )

            )
)

But it comes like this:

Array
(
    [0] => Array
        (
            [valor_apostado] => 100
            [valor_final] => 817
            [id_aposta] => 1022
            [0] => Array
                (
                    [timeApostado] => Motherwell
                    [partidaApostada] => Motherwell x Hearts
                )

        )

The other part of the array inside the array is not displayed.

My code:

$idAnt = 0;
$array = "";
$i = 0;
$n = 0;
while($dado = $pegar->fetch(PDO::FETCH_ASSOC)) {
    $valor_apostado = $dado["valor_apostado"];
    $valor_final = $dado["valor_final"];
    $tipo_aposta = $dado["tipo_aposta"];
    $time = $dado["nome_time"];
    $idAposta = $dado["id_aposta"];

    if($idAposta == $idAnt) {
      $x[$n] = array(
        "timeApostado" => $tipo_aposta,
        "partidaApostada" => $time
      );
      $n++;
    } else {
      $n = 0;
      $x = array(
        "valor_apostado" => $valor_apostado,
        "valor_final" =>$valor_final,
        "id_aposta" => $idAposta,
        $n => array(
          "timeApostado" => $tipo_aposta,
          "partidaApostada" => $time
        )
      );
      $array[$i]= $x;
      $i++;
      $n++;
    }
    $idAnt = $idAposta;

}
    
asked by anonymous 15.02.2017 / 19:45

2 answers

3

Your code seems to be correct, with only an error when assigning the values to the array. You are resetting the counter whenever there is an equal response. Right after the command else remove the condition $n = 0; .

Your code would look like this:

  if($idAposta == $idAnt) {
      $x[$n] = array(
        "timeApostado" => $tipo_aposta,
        "partidaApostada" => $time
      );
      $n++;
    } else {
      $x = array(
    
15.02.2017 / 19:49
2

You can also change your code to use $idAposta as an index.

$array = array();
while($dado = $pegar->fetch(PDO::FETCH_ASSOC)) {
    $valor_apostado = $dado["valor_apostado"];
    $valor_final = $dado["valor_final"];
    $tipo_aposta = $dado["tipo_aposta"];
    $time = $dado["nome_time"];
    $idAposta = $dado["id_aposta"];

    if(array_key_exists($idAposta, $array)) {
        $array[$idAposta][] = array(
            "timeApostado"      => $tipo_aposta,
            "partidaApostada"   => $time
        );
    } else {
        $array[$idAposta] = array(
            "valor_apostado"    => $valor_apostado,
            "valor_final"       =>$valor_final,
            "id_aposta"         => $idAposta,
            0 => array(
                "timeApostado"  => $tipo_aposta,
                "partidaApostada" => $time
            )
        );
    }
}
    
15.02.2017 / 20:01