1

I'm using API to pull soccer data from betsapi. As I'm not expert in JSON, I'm sharing my queries here. My json output looks like this:

{  
"success":1,
"results":{  
  "season":{  
     "start_time":"1502402400",
     "end_time":"1526335199",
     "has_topgoals":"1",
     "has_leaguetable":"1",
     "has_lineups":"1",
     "name_1":"Premier League 17\/18",
     "name":"Premier League 17\/18"
  },

  "overall":{  
     "tournaments":[  
        {  
           "name":"Premier League",
           "rows":[  
              {  
                 "pos":"1",
                 "sort_pos":"1",
                 "change":"0",
                 "win":"8",
                 "draw":"1",
                 "loss":"0",
                 "goalsfor":"32",
                 "goalsagainst":"4",
                 "points":"25",
                 "pct":null,
                 "team":{  
                    "id":"708",
                    "name":"Man City",
                    "image_id":"17",
                    "cc":"gb"
                 }
              }

              {  
                 "pos":"20",
                 "sort_pos":"20",
                 "change":"0",
                 "win":"1",
                 "draw":"0",
                 "loss":"8",
                 "goalsfor":"2",
                 "goalsagainst":"19",
                 "points":"3",
                 "pct":null,
                 "team":{  
                    "id":"17189",
                    "name":"Crystal Palace",
                    "image_id":"7",
                    "cc":"gb"
                 }
              }
           ]
        }
     ]
  },

And my code to print result is

$obj = [''] // code above

$obj = json_decode($data);

foreach($obj as $result){
   echo $result->season->name; // or

   echo $result->overall->tournaments->name; // or
}

Though I've tried it with many alternatives, it doesn't print anything neither does it gives any error.

If anyone can point out the errors, I'd very much appreciate it.

2
  • var_dump() json decoded value. json_decode() will return null if data can't be decoded. Commented Oct 25, 2017 at 13:38
  • @ArtOsi The JSON data output is already there. It's just missing something in looping.. Commented Oct 25, 2017 at 13:45

1 Answer 1

2

How about this:

$obj = json_decode($data, true);
print_r($obj); //Should give you your json converted to php array

Then, to loop through results

foreach ($obj['results'] as $result) {
  echo $result['season']['name'];
}

Also to loop through tournaments:

foreach ($obj['results']['overall']['tournaments'] as $tournament) {
  echo $tournament['name'];
}

To loop through teams in each tournament, try something like this:

foreach ($obj['results']['overall']['tournaments'] as $tournament) {
  echo 'Tournament: ' . $tournament['name'] . '<br />';
  foreach ($tournament['rows'] as $row) {
    echo 'Team: #' . $row['team']['id'] . ' ' . $row['team']['name'] . ' (' . $row['team']['image_id'] . ')' . '<br />';
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Tournaments loop is working though results are still showing blank.. Any hints to loop through the sub-rows in the tournaments. Appreciate your help.
What are you trying to accomplish?
under the tournament 'rows', there is a sub row with "Team'. Can't loop on to get info "image_id". Appreciate for taking time to respond. I'll mark the answer

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.