如何使用PHP处理JSON数组中的值

时间:2015-04-18 13:00:39

标签: php json

我正试图从这段JSON中获取值name

{
  "tournament": {
    "id": 1605872,
    "name": "eBotMatchMakerTest",
    "event_id": null,
    "participants": [
      {
        "participant": {
          "id": 24899481,
          "tournament_id": 1605872,
          "name": "Team1",
          "seed": 1,
          "active": true,
          "created_at": "2015-04-18T07:05:48.601-05:00",
          "updated_at": "2015-04-18T07:05:48.601-05:00",
          "invite_email": null,
          "final_rank": null,
          "misc": null,
          "icon": null,
          "on_waiting_list": false,
          "invitation_id": null,
          "group_id": null,
          "checked_in_at": null,
          "challonge_username": null,
          "challonge_email_address_verified": null,
          "removable": true,
          "participatable_or_invitation_attached": false,
          "confirm_remove": true,
          "invitation_pending": false,
          "display_name_with_invitation_email_address": "Team1",
          "email_hash": null,
          "username": null,
          "display_name": "Team1",
          "attached_participatable_portrait_url": null,
          "can_check_in": false,
          "checked_in": false,
          "reactivatable": false
        }
      }
    ],
    "matches": [
      {
        "match": {
          "id": 36463543,
          "tournament_id": 1605872,
          "state": "pending",
          "player1_id": null,
          "player2_id": null,
          "player1_prereq_match_id": 36463541,
          "player2_prereq_match_id": 36463542,
          "player1_is_prereq_match_loser": false,
          "player2_is_prereq_match_loser": false,
          "winner_id": null,
          "loser_id": null,
          "started_at": null,
          "created_at": "2015-04-18T07:06:12.619-05:00",
          "updated_at": "2015-04-18T07:06:12.619-05:00",
          "identifier": "G",
          "has_attachment": false,
          "round": 3,
          "player1_votes": null,
          "player2_votes": null,
          "group_id": null,
          "attachment_count": null,
          "scheduled_time": null,
          "location": null,
          "underway_at": null,
          "optional": false,
          "prerequisite_match_ids_csv": "36463541,36463542",
          "scores_csv": ""
        }
      }
    ],
    "description_source": "",
    "subdomain": null,
    "full_challonge_url": "http://challonge.com/3k8drpj2",
    "live_image_url": "http://images.challonge.com/3k8drpj2.png",
    "sign_up_url": null,
    "review_before_finalizing": true,
    "accepting_predictions": false,
    "participants_locked": true,
    "game_name": null,
    "participants_swappable": false,
    "team_convertable": false,
    "group_stages_were_started": false
  }
}

我尝试使用$tournament->tournament[0]->name,但它无效。

有人有任何建议吗?

1 个答案:

答案 0 :(得分:7)

使用$variable->tournament->name

Demo

<?php

$json = '{
  "tournament": {
    "id": 1605872,
    "name": "eBotMatchMakerTest",
    "event_id": null,
    "participants": [
      {
        "participant": {
          "id": 24899481,
          "tournament_id": 1605872,
          "name": "Team1",
          "seed": 1,
          "active": true,
          "created_at": "2015-04-18T07:05:48.601-05:00",
          "updated_at": "2015-04-18T07:05:48.601-05:00",
          "invite_email": null,
          "final_rank": null,
          "misc": null,
          "icon": null,
          "on_waiting_list": false,
          "invitation_id": null,
          "group_id": null,
          "checked_in_at": null,
          "challonge_username": null,
          "challonge_email_address_verified": null,
          "removable": true,
          "participatable_or_invitation_attached": false,
          "confirm_remove": true,
          "invitation_pending": false,
          "display_name_with_invitation_email_address": "Team1",
          "email_hash": null,
          "username": null,
          "display_name": "Team1",
          "attached_participatable_portrait_url": null,
          "can_check_in": false,
          "checked_in": false,
          "reactivatable": false
        }
      }
    ],
    "matches": [
      {
        "match": {
          "id": 36463543,
          "tournament_id": 1605872,
          "state": "pending",
          "player1_id": null,
          "player2_id": null,
          "player1_prereq_match_id": 36463541,
          "player2_prereq_match_id": 36463542,
          "player1_is_prereq_match_loser": false,
          "player2_is_prereq_match_loser": false,
          "winner_id": null,
          "loser_id": null,
          "started_at": null,
          "created_at": "2015-04-18T07:06:12.619-05:00",
          "updated_at": "2015-04-18T07:06:12.619-05:00",
          "identifier": "G",
          "has_attachment": false,
          "round": 3,
          "player1_votes": null,
          "player2_votes": null,
          "group_id": null,
          "attachment_count": null,
          "scheduled_time": null,
          "location": null,
          "underway_at": null,
          "optional": false,
          "prerequisite_match_ids_csv": "36463541,36463542",
          "scores_csv": ""
        }
      }
    ],
    "description_source": "",
    "subdomain": null,
    "full_challonge_url": "http://challonge.com/3k8drpj2",
    "live_image_url": "http://images.challonge.com/3k8drpj2.png",
    "sign_up_url": null,
    "review_before_finalizing": true,
    "accepting_predictions": false,
    "participants_locked": true,
    "game_name": null,
    "participants_swappable": false,
    "team_convertable": false,
    "group_stages_were_started": false
  }
}';

$var = json_decode($json);
echo $var->tournament->name;