dtuzjzs3853 2012-09-26 09:03
浏览 9
已采纳

PHP从JSON获取价值

Let's say I have this 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
    }
}

and I am trying this thru PHP:

$string = file_get_contents("achievements.json");
$json_a=json_decode($string,true);

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

I'm trying to get the first "id" of the achievement.. which would be READY FOR WORK.

How do I fix this?

  • 写回答

1条回答 默认 最新

  • doushao6874 2012-09-26 09:06
    关注

    When you set the second parameter of json_decode to true, it will return an array.

    $json_a=json_decode($string,true);
    

    returns an array.

    $getit = $json_a['achievement'][1]['title'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?