2

I have object:

object(stdClass)#6 (4) { 
        [0]=> object(stdClass)#3 (2) { 
                ["name"]=> string(3) "Jan" 
                ["job"]=> string(2) "IT" 
            } 
        [1]=> object(stdClass)#2 (2) { 
                ["name"]=> string(4) "Adam" 
                ["job"]=> string(2) "PR" 
            } 
        [2]=> object(stdClass)#4 (2) { 
                ["name"]=> string(6) "Wojtek" 
                ["job"]=> string(2) "IT" 
            } 
        [3]=> object(stdClass)#5 (2) { 
                ["name"]=> string(6) "Marcin"
                ["job"]=> string(3) "Car" 
            }
    } 

My problem is: how I can use this object:

object(stdClass)#3 (2) { 
        ["name"]=> string(3) "Jan" 
        ["job"]=> string(2) "IT" 
    }

This object is in class.

Something like this: $this->object->object or $this->object->0 does not work.

But when I change index number for example: "0" or "1" to "A" or "B" it works

IMSoP
  • 89,526
  • 13
  • 117
  • 169
grzeso
  • 47
  • 8

2 Answers2

0

You convert multiple arrays to an object with the following code:

$obj1 = (object) ['name' => 'Jan', 'job' => 'IT'];
$obj2 = (object) ['name' => 'Adam', 'job' => 'PR'];
$obj3 = (object) ['name'=> 'Wojtek', 'job' => 'IT'];
$obj4 = (object) ['name' => 'Marcin', 'job' => 'Car'];
$obj = (object) [$obj1, $obj2, $obj3, $obj4];

To get the name of the first object you can cast the object to an array (solution for PHP < 7.2):

echo ((array) $obj)[0]->name; // Jan
echo ((array) $obj)[1]->name; // Adam

Since PHP 7.2 you can also access numeric property names too:

echo $obj->{0}->name; // Jan
echo $obj->{1}->name; // Adam

complete example (with demo):

$obj1 = (object) ['name' => 'Jan', 'job' => 'IT'];
$obj2 = (object) ['name' => 'Adam', 'job' => 'PR'];
$obj3 = (object) ['name'=> 'Wojtek', 'job' => 'IT'];
$obj4 = (object) ['name' => 'Marcin', 'job' => 'Car'];
$obj = (object) [$obj1, $obj2, $obj3, $obj4];

//your output
var_dump($obj);

//PHP < 7.2: get the output of the name of the first object.
echo 'Test: '.((array) $obj)[0]->name; // Jan

//or since PHP 7.2
echo $obj->{0}->name; // Jan

demo: https://3v4l.org/kIVuS

Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
  • @grzeso - Yes I see (already updated) but describe how you get the object too. – Sebastian Brosch Jul 24 '18 at 08:18
  • Sebastian, thanks for your answer, but I create object in other way: `$obj1 = (object) ['name'=>'Jan','job'=>'IT'];$obj2 = (object) ['name'=>'Adam','job'=>'PR'];$obj3 = (object) ['name'=>'Wojtek','job'=>'IT'];$obj4 = (object) ['name'=>'Marcin','job'=>'Car'];$obj = (object) [$obj1, $obj2, $obj3, $obj4];` – grzeso Jul 24 '18 at 08:19
  • Sebastian, thank you very much. I agree with you, it works diferent on PHP < 7.2 and PHP >7.2. I changed PHP Version on my laptop and it work. – grzeso Jul 24 '18 at 09:14
0

Object properties whose names are numbers are actually invalid. That they can be created from certain operations is a long-standing bug.

This bug was finally fixed in PHP 7.2; the 7.2.0 changelog lists several bug reports which are fixed by this change, and links to the RFC which proposed the new behaviour.

You can see the difference in this demo:

$obj = (object)[1,2,3];

var_dump($obj);
var_dump($obj->{"0"});

Prior to PHP 7.2, this produces an object like the one you have, and the property cannot be accessed:

object(stdClass)#1 (3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

Notice: Undefined property: stdClass::$0 in /in/tfbH0 on line 6
NULL

After 7.2, it produces a subtly different object, where the property names are all strings rather than integers, and can be accessed with the ->{"name"} syntax:

object(stdClass)#1 (3) {
  ["0"]=>
  int(1)
  ["1"]=>
  int(2)
  ["2"]=>
  int(3)
}
int(1)

If you can't upgrade your PHP, the only workarounds I know of are:

  • Don't convert arrays with numeric keys to objects; just keep them as arrays.
  • If you are passed such an object by code you can't control, cast it back to an array, with (array)$object.
IMSoP
  • 89,526
  • 13
  • 117
  • 169