最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How to access json_encode result,.? - Stack Overflow

matteradmin2PV0评论

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] => [email protected]
)

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":"[email protected]"}"

and the question is how di I access the array of userData ?
For example I wanna get the username of the userData.

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] => [email protected]
)

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":"[email protected]"}"

and the question is how di I access the array of userData ?
For example I wanna get the username of the userData.

Share Improve this question asked Jan 17, 2012 at 10:19 PradithaPraditha 1,1726 gold badges26 silver badges45 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 4

Since you wrapped your json_encode output in quotes, $userData is a string instead of an object. Losing the quotes will create a Javascript object:

var userData = <?php echo json_encode($user_data); ?>;
alert(userData.username);
userData.session_id;
userData.ip_appress;

and so on.

Don't quote it. Just

var userData = <?php echo json_encode($user_data); ?>;

then you can access userData.session_id and so on.

You simply need to remove the quotes around the string in Javascript. So change:

var userData = '<?php echo json_encode($user_data); ?>';

to:

var userData = <?php echo json_encode($user_data); ?>;

josn_encode() outputs a Javascript object literal, so it is valid Javascript code. By surrounding it with quotes you simply populate a string with this data, but if you remove them it will work.

use it as

<?php
$data = array('index1'=>'value1','index2'=>'value2');
?>
<script>
    var json_data = <?php echo json_encode($data);?>;
    alert(json_data['index1']);
    alert(json_data['index2']);
</script>
<DIV id="test"><?echo json_encode($_POST);?></div>



<div id="ajax">

</div>

<script>
var sample=$("#test").text();
var obj = jQuery.parseJSON(sample);
$("#ajax").load("ceva.php",obj);

</script>

Just a script I used to get data from a div and send it to a script using $_POST. Use jQuery

Post a comment

comment list (0)

  1. No comments so far