4

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();
4
  • It's worth mentioning that variables or property names cannot start with a number, so stdClass::$0 wouldn't really be valid anyway Commented Mar 8, 2013 at 22:10
  • Can you provide a link to the section of the PHP doc that told you this would work? Commented Mar 8, 2013 at 22:13
  • Interesting. It works when you set it using the curly-bracket notation: $array->{0} = 'test1'; print_r($array->{0}); Commented Mar 8, 2013 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... Commented Mar 8, 2013 at 22:19

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

-2

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();

3 Comments

"PHP Fatal error: Cannot use object of type stdClass as array"
i edited my code, try again
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....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.