Add an array inside another

1

Hello, I researched how to add an array inside the other but the result was not quite what I expected. I have the following code:

else if ($_GET['type'] == "listaJogos") {
//echo 'Tipo de operação: ' . $_GET['type'] . '<br>';

$campeonato_id = $_GET['campeonato'];

//Query que retorna a NOME_TIME, ID, DATA_HORA, TB_COTACAO_ID
$query = "SELECT GROUP_CONCAT(timee.nome_time ORDER BY timee.nome_time SEPARATOR ' X ') AS nome_time, 
        partida.id, partida.data_hora, partida.tb_cotacao_id 
        FROM tb_partida AS partida, tb_time AS timee, tb_partida_time AS partidaTime 
        WHERE (partida.id = tb_partida_id && timee.id = tb_time_id) 
        AND (partida.flag_ativo = 1 AND partida.flag_cancelado <> 1 AND partida.flag_finalizado <> 1) 
        AND partida.tb_campeonato_id = $campeonato_id 
        GROUP BY partida.id";

$query2 = "SELECT * FROM 'tb_cotacao' WHERE 'id' = ";


$result = mysqli_query($link, $query);

//--------------------------------------------------------------------------

while ($reg = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $registros[] = array('partida' => $reg);

    $result2 = mysqli_query($link, $query2 . $reg['tb_cotacao_id']);

    //$registros[] = array('partida' => mysqli_fetch_array($result2, MYSQLI_ASSOC));

    array_push($registros, mysqli_fetch_array($result2, MYSQLI_ASSOC));
}

$saida = json_encode(array('json' => $registros));

echo $saida;

}

What I get the following output:

"json": [
{
  "partida": {
    "nome_time": "Acreano\r X Flamengo\r",
    "id": "4",
    "data_hora": "2016-09-03 14:00:00",
    "tb_cotacao_id": "4"
  }
},
{
  "id": "4",
  "casa": "1.23",
  "empate": "2.13",
  "fora": "1.23",
  "gol_meio": "6.00",
  "mais_2gm": "7.00",
  "menos_3gm": "7.67",
  "ambas_marcam": "0.00",
  "casa_empate": "6.00",
  "fora_empate": "7.67",
  "casa_marca": "6.00",
  "fora_marca": "76.00",
  "casa_ou_fora": "76.00",
  "casavence_foramarca": "76.00",
  "foravence_casamarca": "7.00",
  "casavence_zero": "67.00",
  "foravence_zero": "67.00"
}]

I would, however, have the following structure:

"json": [
{
  "partida": {
    "nome_time": "Acreano\r X Flamengo\r",
    "id": "4",
    "data_hora": "2016-09-03 14:00:00",
    "tb_cotacao_id": "4"
    "cotacoes": {
       "id": "4",
       "casa": "1.23",
       "empate": "2.13",
       "fora": "1.23",
       "gol_meio": "6.00",
       "mais_2gm": "7.00",
       "menos_3gm": "7.67",
       "ambas_marcam": "0.00",
       "casa_empate": "6.00",
       "fora_empate": "7.67",
       "casa_marca": "6.00",
       "fora_marca": "76.00",
       "casa_ou_fora": "76.00",
       "casavence_foramarca": "76.00",
       "foravence_casamarca": "7.00",
       "casavence_zero": "67.00",
       "foravence_zero": "67.00"
    }
  }
}]

If anyone can help thank you right away.

    
asked by anonymous 26.12.2016 / 17:23

1 answer

0

Therefore has 2 arrays $ reg
mysqli_fetch_array ($ result2, MYSQLI_ASSOC)

You want to keep 1 array with the following rules:

array(
   'JSON'=>array(
       'partida'=>$reg,
       'cotacoes'=>mysqli_fetch_array($result2, MYSQLI_ASSOC)
   )
)

So the while will be.:

while ($reg = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
   $result2 = mysqli_query($link, $query2 . $reg['tb_cotacao_id']);

   $registros[] = array('partida' => $reg,'cotacoes'=>mysqli_fetch_array($result2, MYSQLI_ASSOC));

}

You should get the result you want.

    
26.12.2016 / 18:00