php - 使用 jquery 操作 JSON 数组

标签 php javascript jquery json

我正在打印 <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/

相关文章:

php - 根据部分字符串选择数组键

php - 通过 $_GET 激活帐户的正确方法是什么?

javascript - 范围 slider - 滑动期间运行功能不起作用?

Javascript 原型(prototype) setter 和 getter

javascript - 如何使用 jQuery 显示多个文件属性?

jQuery 错误 - 无法调用未定义的方法 'indexOf'

php - Doctrine2 原生查询不支持自定义类型

php - MySQL 仅使用 MySQL 获取结果

javascript - 检测浏览器图形性能?

javascript - 在每个 td 选择器之间添加破折号