Mysql / PHP / json_encode boolean string conversion
json, mysql, php
Solution
Well, you are selecting from the database as string. So that's what gets encoded. Use SQL `true`/`false` boolean values, which in PHP become `0`/`1`, and cast them to PHP booleans before JSON encoding them:
$data['id'] = (bool)$data['id']; // 0/1 -> PHP false/true
echo json_encode($data); // {'id':true}
Problem
I have a mysql query lets say: `SELECT cca_id AS id, cca_title AS text,IF((SELECT count(*) from crm_categories WHERE cca_id_prev = id),'TRUE','FALSE') AS children FROM crm_categories WHERE...` now I get an array back with true / false as a string If I use json_encode the result is like `{"id":"false"}` But I need true/false without quotes - the problem is if i use true false in the mysql query as a boolean it returns 0/1 - but I don't want that either... Of course I can run a `str_replace` to the json string - but i think there are alternatives isn't it?