Add one array within another

Asked

Viewed 542 times

1

Hello, I searched how to add an array inside the other one 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;

}

Which brings me to the following exit:

"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, like it to 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, I’d appreciate it.

  • The result is as expected?

  • I managed to solve Tiago, thanks for your help and patience.

  • Good :). If it was with my answer mark as certain, if it was with another option enter the answer and mark as right please.

1 answer

0


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

You want to get 1 array with the following rules.:

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

So while will stay.:

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));

}

It should have the result you want.

  • I forgot to say but I had already tested the merge array_and the result was: "json": [ { "match": { "time_name": "Accreano r X Flamengo r", "id": "4", "time_date": "2016-09-03 14:00:00", "tb_cotaca_id": "4" } } ] ] }

  • That is, the first array has been saved and the second has not been added.

  • @Guilherme Ramalho array_merge does not duplicate Keys updates the value. You can enter in your question the structure you want. Sorry I’m not noticed yet.

  • I changed how I expect the result.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.