1

I am using sports-radar API to get the schedule of NFL week 1. The API returns the following data in json format (I shortened string for example).

"id": "8e45fe2d-fb95-4504-845d-7c815623ccd6",
    "year": 2018,
    "type": "REG",
    "name": "REG",
    "week": {
        "id": "37435167-5cf6-4cce-b405-ff0e264ced9c",
        "sequence": 1,
        "title": "1",
        "games": [{
    "id": "0822b924-eadc-4398-bfe6-83cbbf3a2912",
            "status": "scheduled",
            "reference": "57570",
            "number": 4,
            "scheduled": "2018-09-09T17:00:00+00:00",
            "entry_mode": "INGEST",
            "venue": {
        "id": "6ed18563-53e0-46c2-a91d-12d73a16456d",
                "name": "Lucas Oil Stadium",
                "city": "Indianapolis",
                "state": "IN",
                "country": "USA",
                "zip": "46225",
                "address": "500 South Capitol Avenue",
                "capacity": 67000,
                "surface": "artificial",
                "roof_type": "retractable_dome"
            },
            "home": {
        "id": "82cf9565-6eb9-4f01-bdbd-5aa0d472fcd9",
                "name": "Indianapolis Colts",
                "alias": "IND",
                "game_number": 1
            },
            "away": {
        "id": "ad4ae08f-d808-42d5-a1e6-e9bc4e34d123",
                "name": "Cincinnati Bengals",
                "alias": "CIN",
                "game_number": 1
            },
            "broadcast": {
        "network": "CBS"
            }
        }, {
    "id": "0a456149-c547-4856-9b1b-86e1d93887ae",
            "status": "scheduled",
            "reference": "57574",
            "number": 8,
            "scheduled": "2018-09-09T17:00:00+00:00",
            "entry_mode": "INGEST",
            "venue": {
        "id": "3c85d89a-ec66-4983-acd5-1381d6c8673a",
                "name": "Mercedes-Benz Superdome",
                "city": "New Orleans",
                "state": "LA",
                "country": "USA",
                "zip": "70112",
                "address": "1500 Sugar Bowl Drive",
                "capacity": 73208,
                "surface": "artificial",
                "roof_type": "dome"
            },
            "home": {
        "id": "0d855753-ea21-4953-89f9-0e20aff9eb73",
                "name": "New Orleans Saints",
                "alias": "NO",
                "game_number": 1
            },
            "away": {
        "id": "4254d319-1bc7-4f81-b4ab-b5e6f3402b69",
                "name": "Tampa Bay Buccaneers",
                "alias": "TB",
                "game_number": 1
            },
            "broadcast": {
        "network": "FOX"

I used the following website as a tutorial on how to display only the the data I need and how to loop over it

Note the JSON string is stored in the variable $schedule

MY Code

   // JSON string
      $jsonData = $schedule; //get json string

      // Convert JSON string to Array
      $jsonArray = json_decode($jsonData, true);


      // Convert JSON string to Object
      $jsonObject = json_decode($schedule);

Looping through PHP Array or Object

$someArray = $jsonArray
  foreach ($someArray as $key => $value) {
    echo $value["home"] . ", " . $value["away"] . "<br>";
  }

  // Loop through Object
  $someObject = jsonObject
  foreach($someObject as $key => $value) {
    echo $value->home . ", " . $value->away . "<br>";
  }

My ERRORS

When trying to convert the string to an array and attempting to get the away team name I get the error Illegal string offset 'away' same problem with home and all other data

When trying to access data as an object I get the following error Trying to get property of non-object

I followed the tutorial to the letter. Yet im getting the basic errors above...? Any help and explanation would be appreciated. Thank you

EDIT:

var_export($schedule) returns the following:

array ( 'id' => '8e45fe2d-fb95-4504-845d-7c815623ccd6', 'year' => 2018, 'type' => 'REG', 'name' => 'REG', 'week' => array ( 'id' => '37435167-5cf6-4cce-b405-ff0e264ced9c', 'sequence' => 1, 'title' => '1', 'games' => array ( 0 => array ( 'id' => '0822b924-eadc-4398-bfe6-83cbbf3a2912', 'status' => 'scheduled', 'reference' => '57570', 'number' => 4, 'scheduled' => '2018-09-09T17:00:00+00:00', 'entry_mode' => 'INGEST', 'venue' => array ( 'id' => '6ed18563-53e0-46c2-a91d-12d73a16456d', 'name' => 'Lucas Oil Stadium', 'city' => 'Indianapolis', 'state' => 'IN', 'country' => 'USA', 'zip' => '46225', 'address' => '500 South Capitol Avenue', 'capacity' => 67000, 'surface' => 'artificial', 'roof_type' => 'retractable_dome', ), 'home' => array ( 'id' => '82cf9565-6eb9-4f01-bdbd-5aa0d472fcd9', 'name' => 'Indianapolis Colts', 'alias' => 'IND', 'game_number' => 1, ),
14
  • 1
    Only do this $schedule = json_decode($jsonData, true); and now check var_export($schedule); to check that at what index away is present and then try to adjust foreach() code Commented Sep 4, 2018 at 5:23
  • Ok give me a second to check and ill get back to you. Thank you Commented Sep 4, 2018 at 5:24
  • Do a var_dump($someArray)... Let's see what you've got. Commented Sep 4, 2018 at 5:25
  • You can't convert $value->home and $value->away to string as both are object. Commented Sep 4, 2018 at 5:26
  • your code has $someObject = jsonObject correct it to $someObject = $jsonObject Commented Sep 4, 2018 at 5:26

1 Answer 1

1

You are dealing with array of arrays. Try something like this:

  $someArray = $jsonArray
  foreach ($someArray as $key => $value) {
    echo $value["home"]["name"] . ", " . $value["away"]["name"] . "<br>";
  }
Sign up to request clarification or add additional context in comments.

Comments

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.