Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

This question already has an answer here:

I am just trying to understand why the last two print_r() calls below are not working and throws the following error Undefined property: stdClass::$0. According to the PHP Documentation, I should be able to access the object numeric property using the the following operator $object->{'x'} ( x is the numeric index I want to access ).

Thanks.

$array = (object)array( 
    0 => 'test1',
    1 => 'test2',
    2 => 'test3',
    'test' => (object)array(
        0 => 'hi1',
        1 => 'hi2',
        2 => 'hi3'
    )
);

print_r( $array );

print_r( $array->test );

print_r( $array->test->{'0'} );

print_r( $array->{'0'} );

die();
share|improve this question

marked as duplicate by Francis Avila, Wrikken, hakre, Ocramius, Madara Uchiha Mar 31 '13 at 16:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
It's worth mentioning that variables or property names cannot start with a number, so stdClass::$0 wouldn't really be valid anyway – Adam Elsodaney Mar 8 '13 at 22:10
    
Can you provide a link to the section of the PHP doc that told you this would work? – Barmar Mar 8 '13 at 22:13
    
Interesting. It works when you set it using the curly-bracket notation: $array->{0} = 'test1'; print_r($array->{0}); – MichaelRushton Mar 8 '13 at 22:19
    
Hm, I would previously have said the above should work, but int indexes cannot be accessed this way it seems, later assigned string indexes can: $array->{'0'} = 'something'; & accessing that works, but makes a separate property from the 0(int) property... – Wrikken Mar 8 '13 at 22:19
up vote 2 down vote accepted

This is PHP brokenness. Curly-brace syntax for object property access does not work for all-digit keys.

share|improve this answer

When typecasting an array to an object, all-numeric keys are converted into integer properties; and integer properties cannot be accessed.

$array = array(0 => 'a', 1.5 => 'b', '2' => 'c');

var_dump((object) $array);

// object(stdClass)#1 (3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" }

However, when using curly bracket notation to set all-numeric properties they are stored as strings; which can be accessed.

$array = (object) array();

$array->{0}   = 'a';

$array->{1.5} = 'b';

$array->{'2'} = 'c';

var_dump($array);

// object(stdClass)#1 (3) { ["0"]=> string(1) "a" ["1.5"]=> string(1) "b" ["2"]=> string(1) "c" }

Hence the issue you're having.

share|improve this answer

this is work :

 $array = (object)array( 
    a => 'test1',
    b => 'test2',
    c => 'test3',
    'test' => (object)array(
        a => 'hi1',
        b => 'hi2',
        c => 'hi3'
    )
);

print_r( $array );

print_r( $array->test );

print_r( $array->test->a);

print_r( $array->b );

die();
share|improve this answer
1  
"PHP Fatal error: Cannot use object of type stdClass as array" – Wrikken Mar 8 '13 at 22:10
    
i edited my code, try again – MonTy Adel Mar 8 '13 at 22:22
    
You edited your code to the point you missed the point of the question entirely. Integer keys are the problem (and you have some sloppy code that relies on the constants a, b and c not to be defined. define('a',0);define('b',0);define('c',0);, and see what your code does.... – Wrikken Mar 8 '13 at 22:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.