Using PHP, I'd like to bind a value to a JSON string similar to how it is done when preparing an SQL statement for a database using a question mark. For example, I would like do something like the following:
$v1 = 'v1';
$k2 = 'k2';
$result = json_bind('[{"k1": ?}, {?: "v2"}]', $v1, $k2);
echo $result; // [{"k1": "v1"}, {"k2": "v2"}]
- I don't want to just do a pure string replace as this technique doesn't escape/quote the values properly and it can't format values like arrays and objects properly.
- I don't want to just create an array (or an object) first, assign my params, and use
json_encode
because this gets messy since PHP's syntax differs from JSON's syntax and I need something more dynamic because I may not always know the structure of the JSON.
Is there already a library or function that can do this very thing?