Leo505
Leo505

Reputation: 107

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.??

Upvotes: 1

Views: 45

Answers (1)

jakub wrona
jakub wrona

Reputation: 2254

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

Upvotes: 1

Related Questions