title: PHP "Illegal string offset" error when looping through array
body: <p>I am using the Amazon API to get some XML data, which is then passed through the json_decode() function.</p>
  
  <p>Here are two samples of the data that is returned:</p>
  
  <pre><code>[BrowseNodes] =&gt; Array
      (
          [Request] =&gt; Array
              (
                  [IsValid] =&gt; True
                  [BrowseNodeLookupRequest] =&gt; Array
                      (
                          [BrowseNodeId] =&gt; 2645269011
                          [ResponseGroup] =&gt; BrowseNodeInfo
                      )
  
              )
  
          [BrowseNode] =&gt; Array
              (
                  [BrowseNodeId] =&gt; 2645269011
                  [Name] =&gt; Featured Categories
                  [Children] =&gt; Array
                      (
                          [BrowseNode] =&gt; Array
                              (
                                  [0] =&gt; Array
                                      (
                                          [BrowseNodeId] =&gt; 3741261
                                          [Name] =&gt; Cooktops
                                      )
  
                                  [1] =&gt; Array
                                      (
                                          [BrowseNodeId] =&gt; 3741271
                                          [Name] =&gt; Dishwashers
                                      )
  
                                  [2] =&gt; Array
                                      (
                                          [BrowseNodeId] =&gt; 3741331
                                          [Name] =&gt; Freezers
                                      )
  
                                  [3] =&gt; Array
                                      (
                                          [BrowseNodeId] =&gt; 2399939011
                                          [Name] =&gt; Ice Makers
                                      )
  
                              )
  
                      )
  </code></pre>
  
  <p>and</p>
  
  <pre><code>[BrowseNodes] =&gt; Array
      (
          [Request] =&gt; Array
              (
                  [IsValid] =&gt; True
                  [BrowseNodeLookupRequest] =&gt; Array
                      (
                          [BrowseNodeId] =&gt; 3774781
                          [ResponseGroup] =&gt; BrowseNodeInfo
                      )
  
              )
  
          [BrowseNode] =&gt; Array
              (
                  [BrowseNodeId] =&gt; 3774781
                  [Name] =&gt; Vitamin D
                  [Children] =&gt; Array
                      (
                          [BrowseNode] =&gt; Array
                              (
                                  [BrowseNodeId] =&gt; 6936848011
                                  [Name] =&gt; D3
                              )
  
                      )
  </code></pre>
  
  <p>I am then using this code to obtain data for each of the children:</p>
  
  <pre><code>if(isset($result['BrowseNodes']['BrowseNode']['Children'])){
  $childs = $result['BrowseNodes']['BrowseNode']['Children']['BrowseNode'];
          foreach($childs as $child){
              $browsenodeid = $child['BrowseNodeId'];
              $name = $child['Name'];
          }
      }
  </code></pre>
  
  <p>Everything works fine in the first sample, but in the second one, I am getting the following errors:</p>
  
  <p><strong>Warning: Illegal string offset 'BrowseNodeId'</strong></p>
  
  <p><strong>Warning: Illegal string offset 'Name'</strong></p>
  
  <p>If I echo $browsenodeid and $name in the second example, it gives me 6 and D as the output.</p>
  
  <p>Any ideas as to what I am doing wrong? I am quite confused; to me the data I am looping through in both cases seems to be the same, the only difference being that in the first case, the array has 4 elements and in the second it has 1 element.</p>
  
  <p>Thanks in advance.</p>