title: decoding an array from json into PHP and unable to access elements of array using keys
body: <p>I have some JSON similar to the following:</p>
  
  <pre><code>{"internalArray": {"201": "A", "202": "B", "5": "C", "46": "D"}, 
   "data": "ABCDEFG", 
   "data2": "TSXPIIF"}
  </code></pre>
  
  <p>I use the following PHP code to decode it:</p>
  
  <pre><code>$jsonOutput = json_decode($output);
  </code></pre>
  
  <p>I would like to get access to the "internalArray" from the JSON data, so I reference it using the following:</p>
  
  <pre><code>$internalArray = $jsonOutput-&gt;{'internalArray'};
  </code></pre>
  
  <p>When I do a var_dump on $internalArray</p>
  
  <pre><code>object(stdClass)#4 (4) 
  { ["201"]=&gt; string(1) "A" 
       ["202"]=&gt; string(1) "B" 
       ["5"]=&gt; string(1) "C" 
       ["46"]=&gt; string(1) "D" 
  } 
  </code></pre>
  
  <p>I figured out that I could cast this to an array, so I did the following:</p>
  
  <pre><code>$internalArray = (array) $jsonOutput-&gt;{'internalArray'};
  </code></pre>
  
  <p>However, now that I have this array, I can't appear to access it using values like</p>
  
  <pre><code>$internalArray["202"], $internalArray["201"], etc.
  </code></pre>
  
  <p>When I try to access it via the keys, it returns NULL.  However, when I have code like this:</p>
  
  <pre><code>foreach ($internalArray as $key =&gt; $value)
  {
    echo $key . "," . $value;
  }
  </code></pre>
  
  <p>it prints out the values as expected, like "202,A", etc.</p>
  
  <p>However, in the same code if I change it to </p>
  
  <pre><code>foreach ($internalArray as $key =&gt; $value)
  {
    echo $key . "," . $internalArray[$key];
  }
  </code></pre>
  
  <p>it doesn't work!</p>
  
  <p>Can anyone explain why I can't access the values in $internalArray using the keys?  Am I doing something fundamentally wrong here?</p>