Prevent quoting of certain values with PHP json_encode()

后端 未结 7 1455
广开言路
广开言路 2021-01-06 01:21

When using PHP\'s json_encode to encode an array as a JSON string, is there any way at all to prevent the function from quoting specific values in the returned string? The r

7条回答
  •  攒了一身酷
    2021-01-06 01:39

    No, json_encode can't do that. You need to construct your JS expression by hand then:

    $json = "{'special':" . json_encode($string) . " + js_var,"
          .  "'value': 123}";
    

    (Try to still use json_encode for fixed value parts, like in above example.)

提交回复
热议问题