Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8656197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:13:12+00:00 2026-06-12T15:13:12+00:00

I have some JSON similar to the following: {internalArray: {201: A, 202: B, 5:

  • 0

I have some JSON similar to the following:

{"internalArray": {"201": "A", "202": "B", "5": "C", "46": "D"}, 
 "data": "ABCDEFG", 
 "data2": "TSXPIIF"}

I use the following PHP code to decode it:

$jsonOutput = json_decode($output);

I would like to get access to the “internalArray” from the JSON data, so I reference it using the following:

$internalArray = $jsonOutput->{'internalArray'};

When I do a var_dump on $internalArray

object(stdClass)#4 (4) 
{ ["201"]=> string(1) "A" 
     ["202"]=> string(1) "B" 
     ["5"]=> string(1) "C" 
     ["46"]=> string(1) "D" 
} 

I figured out that I could cast this to an array, so I did the following:

$internalArray = (array) $jsonOutput->{'internalArray'};

However, now that I have this array, I can’t appear to access it using values like

$internalArray["202"], $internalArray["201"], etc.

When I try to access it via the keys, it returns NULL. However, when I have code like this:

foreach ($internalArray as $key => $value)
{
  echo $key . "," . $value;
}

it prints out the values as expected, like “202,A”, etc.

However, in the same code if I change it to

foreach ($internalArray as $key => $value)
{
  echo $key . "," . $internalArray[$key];
}

it doesn’t work!

Can anyone explain why I can’t access the values in $internalArray using the keys? Am I doing something fundamentally wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:13:14+00:00Added an answer on June 12, 2026 at 3:13 pm

    If you want an associative array, you can ask PHP for an associative array (see documentation for json_decode):

    $jsonOutput = json_decode($output, true);
    
    var_dump($jsonOutput['internalArray']);
    

    Produces:

    array(4) {
      [201]=>
      string(1) "A"
      [202]=>
      string(1) "B"
      [5]=>
      string(1) "C"
      [46]=>
      string(1) "D"
    }
    

    Back to your issue, your code would still work if the keys in the internal array were not numeric. What is happening here is a little peculiar: PHP doesn’t allow you to have numeric strings (eg: '201', '46') as keys for an array.

    Numeric strings keys will be converted to numbers keys instead. So when you do $arr['201'] PHP will look for $arr[201] instead. However, when you cast your object into an array, it looks like the array keys remain strings (eg: $arr['201']). Now the actual array has a numeric string key, but whenever you try to access it, PHP looks for an int key and never finds it, giving you NULL.

    In fact, the documentation notes that:

    If an object is converted to an array, the result is an array whose elements are the object’s properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; (…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a structure similar to the following: /root/ /root/data/ /root/data/script.php /root/data/some.json /root/data/feature/one.json /root/data/feature/two.json
I have some json files with 500MB. If I use the "trivial" json.load() to
Inside my .aspx I have some JSON code that looks like this: function someFunctionName()
I have some data and need to create a json file with this structure
I have some data that I have to serialize to JSON. I'm using JSON.NET.
I have a data() object containing some json. Is there a way I can
I have a REST api which dumps some json data (user info, etc). Now
I have a code similar to this: $.ajax({ success: function(data) { text = '';
I'm using GWT 2.3 and I have json-p requests in my code similar to
I am trying to use use the following JSON data to create the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.