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 8551177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:08:17+00:00 2026-06-11T14:08:17+00:00

I have a PHP array that has numeric keys as a string type. But

  • 0

I have a PHP array that has numeric keys as a string type.

But when I try and access them PHP is giving me an undefined index error.

$a = (array)json_decode('{"1":1,"2":2}');
var_dump($a);
var_dump(isset($a[1]));
var_dump(isset($a["1"]));
var_dump($a[1]);
var_dump($a["1"]);

Output:

array (size=2)
    '1' => int 1
    '2' => int 2

boolean false

boolean false

ERROR: E_NOTICE: Undefined offset: 1

null

ERROR: E_NOTICE: Undefined offset: 1

null

How do I access these values?

Demo: http://codepad.viper-7.com/8O03IM

  • 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-11T14:08:20+00:00Added an answer on June 11, 2026 at 2:08 pm

    So, I haven’t seen any other answers touch upon this, but @xdazz came close.

    Let’s start our environment, $obj equals the object notation of a decoded string:

    php > $obj = json_decode('{"1":1,"2":2}');
    
    php > print_r($obj);
    stdClass Object
    (
        [1] => 1
        [2] => 2
    )
    
    php > var_dump( $obj );
    object(stdClass)#1 (2) {
      ["1"]=>
      int(1)
      ["2"]=>
      int(2)
    }
    

    If you want to access the strings, we know the following will fail:

    php > echo $obj->1;
    
    Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `'{'' or `'$'' in php shell code on line 1
    

    Accessing the object variables

    You can access it like so:

    php > echo $obj->{1};
    1
    

    Which is the same as saying:

    php > echo $obj->{'1'};
    1
    

    Accessing the array variables

    The issue with arrays is that the following return blank, which is the issue with typecasting.

    php > echo $obj[1];
    php >
    

    If you typecast it back, the object is once again accessible:

    php > $obj = (object) $obj;
    php > echo $obj->{1};
    1
    

    Here is a function which will automate the above for you:

    function array_key($array, $key){
        $obj = (object) $array;
        return $obj->{$key};
    }
    

    Example usage:

    php > $obj = (array) $obj;
    php > echo array_key($obj, 1);
    1
    
    php > echo array_key($obj, 2);
    2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php array that has a bunch of data that I need
I have a massive multidimensional array that has been serialised by PHP. It has
I have a PHP array that I'd like to duplicate but only copy elements
I have a PHP array that has literal_key => value. I need to shift
I have an array that has been serialized by php, the result is: unserialize('a:2:{s:13:custom_basket;a:1:{i:280583837398;a:4:{s:12:product_name;s:0:;s:8:quantity;s:1:1;s:5:price;d:38.649999999999999;s:11:description;a:7:{s:2:id;s:12:280583837398;s:3:sku;s:0:;s:4:site;s:2:UK;s:12:condition_id;s:4:1000;s:14:transaction_id;s:12:773563256018;s:8:platform;s:4:eBay;s:18:order_line_item_id;s:25:280583837398-773563256018;}}}s:6:basket;a:0:{}}')
I have an PHP array that has saved users steps: array( 'step_1' => 1,
I have a PHP array that looks like this: Array{ [0] { 'id' =>
In PHP I have an array that looks like this: $array[0]['width'] = '100'; $array[0]['height']
I have php code that puts some values into an Array as follows: $hunter=addslashes($MessageArray[1]);
I have an multi-dimensional array that I want to send to a PHP script

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.