1
我正在使用高級自定義字段轉發器來動態創建要放置在Google地圖上的位置列表。我試圖將我的PHP數組轉換爲JSON,以便我可以訪問我的Google Maps腳本文件中的位置。我遇到的問題是帶有撇號的位置在Javascript中導致錯誤。我知道撇號需要逃避,但我不知道如何動態地做到這一點。將PHP數組轉換爲JSON時動態轉義撇號
這是我的PHP:
$locations = get_field('locations');
$jsonLocations = json_encode($locations);
然後我試圖在PHP數組傳遞給使用Javascript,像這樣:
var jsonLocations = '<?php echo $jsonLocations; ?>';
這是什麼導致了我的問題。我嘗試使用utf8_encode
根據another question I found on here,但這給了我一個null
結果。
$locations = get_field('locations');
$jsonLocations = utf8_encode($locations);
$jsonLocations = json_encode($jsonLocations);
JSON_HEX_QUOT,JSON_HEX_APOS http://php.net/manual/en/function.json-encode.php – bassxzero
@bassxzero謝謝你,成功了!請添加爲答案。 – user13286