title: Getting JSON data from API as array or object
body: <p>I am using sports-radar API to get the schedule of NFL week 1. The API returns the following data in <code>json</code> format (I shortened string for example).</p>
  
  <pre><code>"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"
  </code></pre>
  
  <p><a href="https://jonsuh.com/blog/convert-loop-through-json-php-javascript-arrays-objects/" rel="nofollow noreferrer">I used the following website</a> as a tutorial on how to display only the the data I need and how to loop over it</p>
  
  <p>Note the JSON string is stored in the variable <code>$schedule</code></p>
  
  <p><strong>MY Code</strong></p>
  
  <pre><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);
  </code></pre>
  
  <p><strong>Looping through PHP Array or Object</strong></p>
  
  <pre><code>$someArray = $jsonArray
    foreach ($someArray as $key =&gt; $value) {
      echo $value["home"] . ", " . $value["away"] . "&lt;br&gt;";
    }
  
    // Loop through Object
    $someObject = jsonObject
    foreach($someObject as $key =&gt; $value) {
      echo $value-&gt;home . ", " . $value-&gt;away . "&lt;br&gt;";
    }
  </code></pre>
  
  <p><strong>My ERRORS</strong></p>
  
  <p>When trying to convert the string to an array and attempting to get the <code>away</code> team name  I get the error <strong>Illegal string offset</strong> 'away' same problem with <code>home</code> and all other data </p>
  
  <p>When trying to access data as an object I get the following error <code>Trying to get property of non-object</code></p>
  
  <p>I followed the tutorial to the letter. Yet im getting the basic errors above...? Any help and explanation would be appreciated. Thank you</p>
  
  <p>EDIT:</p>
  
  <p><code>var_export($schedule)</code> returns the following:</p>
  
  <pre><code>array ( 'id' =&gt; '8e45fe2d-fb95-4504-845d-7c815623ccd6', 'year' =&gt; 2018, 'type' =&gt; 'REG', 'name' =&gt; 'REG', 'week' =&gt; array ( 'id' =&gt; '37435167-5cf6-4cce-b405-ff0e264ced9c', 'sequence' =&gt; 1, 'title' =&gt; '1', 'games' =&gt; array ( 0 =&gt; array ( 'id' =&gt; '0822b924-eadc-4398-bfe6-83cbbf3a2912', 'status' =&gt; 'scheduled', 'reference' =&gt; '57570', 'number' =&gt; 4, 'scheduled' =&gt; '2018-09-09T17:00:00+00:00', 'entry_mode' =&gt; 'INGEST', 'venue' =&gt; array ( 'id' =&gt; '6ed18563-53e0-46c2-a91d-12d73a16456d', 'name' =&gt; 'Lucas Oil Stadium', 'city' =&gt; 'Indianapolis', 'state' =&gt; 'IN', 'country' =&gt; 'USA', 'zip' =&gt; '46225', 'address' =&gt; '500 South Capitol Avenue', 'capacity' =&gt; 67000, 'surface' =&gt; 'artificial', 'roof_type' =&gt; 'retractable_dome', ), 'home' =&gt; array ( 'id' =&gt; '82cf9565-6eb9-4f01-bdbd-5aa0d472fcd9', 'name' =&gt; 'Indianapolis Colts', 'alias' =&gt; 'IND', 'game_number' =&gt; 1, ),
  </code></pre>