Leo505 Leo505 - 2 months ago 6
JSON Question

PHP index array for JSON

Looking for some help.. Don't know the best approach to this issue...
I'm pushing a new reference onto an array but the "true" value is being inserted with quotes, which fails my json format.

while( $row = mysqli_fetch_assoc($res) ) {
if($row['id']=="2"){
$row['children']= 'true';
}
$data[] = $row;
}
echo json_encode( $data);


Outputs

[{"id":"2","name":"john","text":"john","parent_id":"0","children":"true"}]


When i need...

{"id":"2","name":"john","text":"john","parent_id":"0","children":true}]


How would i go about removing the qoutes or inserting it correctly first.??

Answer

If you want the 'children' to be boolean then set it to boolean.

while( $row = mysqli_fetch_assoc($res) ) { if($row['id']=="2"){ $row['children'] = true; } $data[] = $row; } echo json_encode( $data);