简体   繁体   中英

php array to javascript using json_encode()

I have a php array $presentation, and I am trying to assign it to a JavasScript variable. when I use json_encode i got this value:

{"image":"\/ara\/web\/uploads\/images\/slide\/34.jpeg","sound":"\/ara\/web\/uploads\/images\/slide\/34.mpga","content":"sentence"}

you see the back and forward slashes?

and this gets worst when I try to assign the variable to a javascript one: I get this:

 "books":[{"image":"\/ara\/web\/uploads\/images\/slide\/32.jpeg"

and this is the code:

php:

$presentation_slide = json_encode($presentation_slides)

javascript:

<script>
    var presentation = {{ presentation_slides }}
</script>

I am using sumfony2

Maybe you should use the JSON_UNESCAPED_SLASHES option:

http://php.net/manual/en/function.json-encode.php

$presentation = json_encode($presentation_slides, JSON_UNESCAPED_SLASHES);

The json_encoding function has numerous flags that you can pass to it, which enable the function to parse particular character sets. The following call ought to solve the problems you are having

json_encode($presentation_slides, JSON_UNESCAPED_SLASHES | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP );

$presentation = json_encode($presentation_slides, JSON_UNESCAPED_SLASHES);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM