Doubt PHP Json Array

0

In the query below

$id_candidato = 1;
$id_vaga = 2; 

$sql = $mysqli->query("SELECT * FROM candidatos WHERE id_vaga = '".$id_vaga."' AND id_candidato = '".$id_candidato."'");

if($sql->num_rows > 0){
    while($row = $sql->fetch_array(MYSQLI_BOTH)){
        $registro = array(
            "ID" => $row['id'],
            "VAGA" => $row['id_vaga'],
            "ID_CANDIDATO" => $row['id_candidato']
        );

        $retorno[] = $registro;

    }
    $mysqli->close();
    $retorno = json_encode($retorno);
    echo $retorno;

}else {
    echo "nao existe";
}

I get the following return:

[{"ID":"1","VAGA":"2","ID_CANDIDATO":"1"},{"ID":"2","VAGA":"2","ID_CANDIDATO":"1"}]

My question is how do I get the return displayed as below?

[{"ID":"1","VAGA":"2","ID_CANDIDATO":"1", "ID":"2","VAGA":"2","ID_CANDIDATO":"1"}]
    
asked by anonymous 30.06.2017 / 22:03

1 answer

0

Well, I realized that making nested arrays can do the loop correctly in angularjs. I do not know if this is the best way to write the script but it looks like this:

$id_candidato = 1;
$id_vaga = 2; 

$sql = $mysqli->query("SELECT * FROM candidatos WHERE id_vaga = '".$id_vaga."' AND id_candidato = '".$id_candidato."'");

if($sql->num_rows > 0){
  while($row = $sql->fetch_array(MYSQLI_BOTH)){
    $registro = array(
        "ID" => $row['id'],
        "VAGA" => [$row['id_vaga']],
        "ID_CANDIDATO" => $row['id_candidato']
    );

    $retorno[] = $registro;

}
  $mysqli->close();
  $retorno = json_encode($retorno);
  echo $retorno;

}else {
  echo "nao existe";
}

That brings me as a result

[{"ID":"1","VAGA":["2"],"ID_CANDIDATO":"1"},{"ID":"2","VAGA":["2"],"ID_CANDIDATO":"1"}]
    
01.07.2017 / 22:47