I have a json object that looks something like this:
Array (
[algorithm] => HMAC-SHA256
[expires] => 1341331200
[issued_at] => 1341326884
[oauth_token] => AAADmtzfo7M4BAEjaqxcD5ofrPIlbAqn6WVV9Az63C5uRxZACrPnkvWddolF9cTW82g13ZAZCcc9z4RBFLZBAFwuflZB1NZAZBI2ZBFqLrm9KQQZDZD
[user] => Array (
[country] => se
[locale] => en_US
[age] => Array (
[min] => 21 )
)
[user_id] => 651666483
)
)
(printed using print_r) I want to add it to a javascript variable. by printing it to the html. What’s the best way to do this?
Just use
json_encode()(PHP docu) to transform your object into a JSON object, which can directly be used as a object/array-literal in JavaScript.