anyavacy
anyavacy

Reputation: 1697

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

Upvotes: 0

Views: 145

Answers (3)

delpha
delpha

Reputation: 970

$presentation = json_encode($presentation_slides, JSON_UNESCAPED_SLASHES);

Upvotes: 0

Alex
Alex

Reputation: 1573

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 );

Upvotes: 1

DigiLive
DigiLive

Reputation: 1103

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);

Upvotes: 1

Related Questions