1

I'm trying to display multiple values, but unfortunately I cannot find an answer to my problem. I am only able to display one value and I'm sure it's a simple answer, I just cannot find it.

SAMPLE JSON:

[
  {
    "match": {
      "attachment_count": null,
      "created_at": "2015-01-19T16:57:17-05:00",
      "group_id": null,
      "has_attachment": false,
      "id": 23575258,
      "identifier": "A",
      "location": null,
      "loser_id": null,
      "player1_id": 16543993,
      "player1_is_prereq_match_loser": false,
      "player1_prereq_match_id": null,
      "player1_votes": null,
      "player2_id": 16543997,
      "player2_is_prereq_match_loser": false,
      "player2_prereq_match_id": null,
      "player2_votes": null,
      "round": 1,
      "scheduled_time": null,
      "started_at": "2015-01-19T16:57:17-05:00",
      "state": "open",
      "tournament_id": 1086875,
      "underway_at": null,
      "updated_at": "2015-01-19T16:57:17-05:00",
      "winner_id": null,
      "prerequisite_match_ids_csv": "",
      "scores_csv": ""
    }
  },
  {
    "match": {
      "attachment_count": null,
      "created_at": "2015-01-19T16:57:17-05:00",
      "group_id": null,
      "has_attachment": false,
      "id": 23575259,
      "identifier": "B",
      "location": null,
      "loser_id": null,
      "player1_id": 16543994,
      "player1_is_prereq_match_loser": false,
      "player1_prereq_match_id": null,
      "player1_votes": null,
      "player2_id": 16543996,
      "player2_is_prereq_match_loser": false,
      "player2_prereq_match_id": null,
      "player2_votes": null,
      "round": 1,
      "scheduled_time": null,
      "started_at": "2015-01-19T16:57:17-05:00",
      "state": "open",
      "tournament_id": 1086875,
      "underway_at": null,
      "updated_at": "2015-01-19T16:57:17-05:00",
      "winner_id": null,
      "prerequisite_match_ids_csv": "",
      "scores_csv": ""
    }
  },
  {
    "match": {
      "attachment_count": null,
      "created_at": "2015-01-19T16:57:17-05:00",
      "group_id": null,
      "has_attachment": false,
      "id": 23575260,
      "identifier": "C",
      "location": null,
      "loser_id": null,
      "player1_id": null,
      "player1_is_prereq_match_loser": false,
      "player1_prereq_match_id": 23575258,
      "player1_votes": null,
      "player2_id": null,
      "player2_is_prereq_match_loser": false,
      "player2_prereq_match_id": 23575259,
      "player2_votes": null,
      "round": 2,
      "scheduled_time": null,
      "started_at": null,
      "state": "pending",
      "tournament_id": 1086875,
       "underway_at": null,
      "updated_at": "2015-01-19T16:57:17-05:00",
      "winner_id": null,
      "prerequisite_match_ids_csv": "23575258,23575259",
      "scores_csv": ""
    }
   }
]

I'm using the following to retrieve "state", but I also need to retrieve "player1_id", "player2_id", "scores_csv" and other items.

<?php
$apikey = 'MYAPIKEY';
$tournamenturl = file_get_contents('http://192.168.4.84/currenturl.php');
//print $tournamenturl;
$api_url = "http://api.challonge.com/v1/tournaments/$tournamenturl/matches.json?api_key=$apikey";
//print $api_url;
$contents_api = fopen($api_url,"r");
$json = stream_get_contents($contents_api);
$jsonData = json_decode($json,TRUE);
//print_r ($jsonData);
foreach ($jsonData as $key => $jsons) { 
     foreach($jsons as $key => $value) {
        foreach($value as $key => $state) {
                if($key == 'state'){
                        echo "$state <br>";{ 
    }
   }
  }
 }
}
?>

I'm certain there has to be a better way to go about doing this, however I just can't seem to stumble upon an answer that makes sense to me to return the output:

"state: complete"; "player1_id: 16543994"; "player2_id: 12345678"; "scores_csv: 3,2";

Any help would be greatly appreciated.

1 Answer 1

1

Instead of nested foreach(), just use array_column() and a single foreach() to get your data:

$jsonData = json_decode($json,true);
$finalJsonArr = array_column($jsonData,'match');

foreach($finalJsonArr as $arr){
    echo "state:".$arr['state']."\n";
    echo "player1_id:".$arr['player1_id']."\n";
    echo "player2_id:".$arr['player2_id']."\n";
    echo "scores_csv:".$arr['scores_csv']."\n";
    echo "\n\n\n";
}

Sample output (using your input data): https://3v4l.org/sbYST

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I knew there had to be an easier way! Absolutely solved my problem!

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.