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();
Nick Fury
  • 1,313
  • 3
  • 13
  • 23
Guillaume Lavoie
  • 567
  • 4
  • 19
  • 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 08 '13 at 22:10
  • Can you provide a link to the section of the PHP doc that told you this would work? – Barmar Mar 08 '13 at 22:13
  • Interesting. It works when you set it using the curly-bracket notation: `$array->{0} = 'test1'; print_r($array->{0});` – Michael Mar 08 '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 08 '13 at 22:19

3 Answers3

3

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

Community
  • 1
  • 1
Francis Avila
  • 31,233
  • 6
  • 58
  • 96
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.

Michael
  • 11,912
  • 6
  • 49
  • 64
-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();
Mina Atia
  • 780
  • 1
  • 4
  • 13
  • 1
    _"PHP Fatal error: Cannot use object of type stdClass as array"_ – Wrikken Mar 08 '13 at 22:10
  • i edited my code, try again – Mina Atia Mar 08 '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 08 '13 at 22:34