0

I'm trying to get the value name from this piece of 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
  }
}

I have tried using $tournament->tournament[0]->name but it is not working.

Does anyone have any advice?

1

1 Answer 1

7

Use $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;
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, thanks! This worked perfectly! Can I also do it for the participants as well?
They will be even more nested but this should give you the idea of how to do it
1 more question, if I have multiple participant arrays, how can I go through each one of them?
Nevermind, found out myself. Thanks for the help! I will set your answer as the correct one when I can.
@cheese5505 Accept john's answer & please dont add chain of questions in comments it will be helpful for others if u post it as a new question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.