I’ve the following array in PHP:
$user_data = Array
(
[session_id] => 30a6cf574ebbdb11154ff134f6ccf4ea
[ip_address] => 127.0.0.1
[user_agent] => Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
[last_activity] => 1326795168
[user_id] => 1
[username] => praditha
[logged_in] => 1
[user_status] => 1
[email] => xxx@yyy.com
)
and then in javaScript, I encode that array to json using:
var userData = '<?php echo json_encode($user_data); ?>';
and I using firebug to see userData value and this is the result:
"{"session_id":"30a6cf574ebbdb11154ff134f6ccf4ea","ip_address":"127.0.0.1","user_agent":"Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1","last_activity":1326795168,"user_id":"1","username":"praditha","logged_in":true,"user_status":"1","email":"xxx@yyy.com"}"
and the question is how di I access the array of userData ?
For example I wanna get the username of the userData.
Since you wrapped your
json_encodeoutput in quotes,$userDatais a string instead of an object. Losing the quotes will create a Javascript object: