php - PHP从JSON获取值(value)

标签 php json

假设我有这个JSON:

{
  "achievement": [
    {
      "title": "Ready for Work",
      "description": "Sign up and get validated",
      "xp": 50,
      "difficulty": 1,
      "level_req": 1
    },
    {
      "title": "All Around Submitter",
      "description": "Get one piece of textual content approved in all five areas.",
      "xp": 500,
      "difficulty": 2,
      "level_req": 1
    }
}

而且我正在尝试通过PHP:
$string = file_get_contents("achievements.json");
$json_a=json_decode($string,true);

$getit = $json_a->achievement['title'][1];

我正在尝试获取成就的第一个“id”,即READY FOR WORK

我该如何解决?

最佳答案

当您将json_decode的第二个参数设置为true时,它将返回一个数组。

$json_a=json_decode($string,true);

返回一个数组。
$getit = $json_a['achievement'][1]['title'];

关于php - PHP从JSON获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12598149/

相关文章:

php - 切换表时出现 zend db 错误可捕获的 fatal error : Argument 1 passed to __construct() must be an array, 给定对象,调用

php - 在完整的 MySQL 表上运行正则表达式搜索的最佳方式?

jquery - jQuery Ajax未被捕获的TypeError : Cannot use 'in' operator to search

json - 如何在集成请求 (AWS APIGateway) 中取消转义请求路径

php - Laravel 忽略查询中的多种类型的对象

php - 如何解决phpmyadmin中的超时问题?

php - array_map 与 str_replace

java - 测试时密码的 JSON 序列化

java - 放入 JSON 对象中的变量

json - Spring 3.1.2中带有JAXB批注的注册器MappingJackson2HttpMessageConverter