我正在打印 <ul>
数组 $_SESSION['words']
在 PHP 中。
// PHP print new words as links
echo '<ul id="wordlist">';
foreach($_SESSION['words'] as $word => $description) {
echo '<li id="wordlist"><a href="#">' . $word . '</a></li>';
}
数组很简单 - 单词存储为键,单词的描述将是值,例如
array(5) { ["word1"]=> int(1) ["word2"]=> int(2) ["word3"]=> int(3) ["word4"]=> int(4) ["word5"]=> int(5) }
我计划通过选择 $word
来操作数组中的项目并添加 $description
并将它添加到数组中,所以我想我会使用 jQuery 来完成它。
我的问题是:如果我从这个 PHP 数组创建一个 JSON 数组,我可以用同样的方式打印出来并操作它吗?我用 $myJSON = json_encode($_SESSION['words']);
编码了它但我已经尝试从 JSON 中打印出键/值!谢谢...
最佳答案
您可以使用 $.parseJSON
:
var json = $.parseJSON('your json');
alert(json.word1);
alert(json.word2);
如果您的 JSON 数据可通过 URL 获得,您可以使用 $.getJSON
关于php - 使用 jquery 操作 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10863728/