JSON manipulation

0

I have the following code:

$Json = array();

foreach ($Dados as $ln){
    $Json[label] = $ln['NOME'];
    $Json[value] = $ln['EMAIL'];            
}

echo json_encode($Json);

With this I have the following return:

{"label":"xxxxxxxxx","value":"xxxxxxxxxxx"}

I would like to bring all returns from the bank and also remove the quotes from both the label and the value.

Expected result:

[
  {label:"xxxxxxxxx",value:"xxxxxxxxxxx"}
  {label:"yyyyyyyyy",value:"yyyyyyyyyyy"}
  {label:"zzzzzzzzz",value:"zzzzzzzzzzz"}
]
    
asked by anonymous 07.03.2016 / 19:45

1 answer

2

You can solve this by creating an index variable to group the label and value.

$Json = array();
$i = 0;
foreach ($Dados as $ln){
    $Json[$i]['label'] = $ln['NOME'];
    $Json[$i]['value'] = $ln['EMAIL'];            
    $i++;
}

echo json_encode($Json);

Depending on the case, just assigning the current item to a new position in the array resolves:

foreach ($Dados as $ln){
    $Json[] = $ln;
}
echo json_encode($Json);
    
07.03.2016 / 19:54