stdClass Object ( [0] => Array ( [0] => stdClass Object ( [id] => 123445566 [name] => Sharer Name [offset] => 0 [length] => 14 [type] => user ) ) [23] => Array ( [0] => stdClass Object ( [id] => 123456778_page_id_i_want [name] => Page Name [offset] => 23 [length] => 14 [type] => page ) ) )

Given the above, how do l access 123456778_page_id_i_want and Page Name

4 Years
Discussion Span
Last Post by blocblue

With whats given I don't know what your initial object variable is (what your var_dump'd) so I'll just use $objVar for demonstration purposes.

Easiest way is to work backwards from what you want. So for example the id. We see it's keyed as id so we write that down. Step back one and wee see id is part of an object so we use the -> connector. Step back and we see the object is part of something keyed as 0 so we write that and stepping back one more we see 0 is part of an array so we surround it with [] so we have [0]->id. Step back again and we see that the array is part of something keyed as 23 so we write donw 23[0]->id stepping back again we see that 23 is part of the main object we dumped sp we use the -> connector and add our object variable to get $objVar->23[0]->id

So in the end we have:

$objVar->23[0]->id; //123456778_page_id_i_want
$objVar->23[0]->name; //Page Name


@GliderPilot - I do not believe that you can access an object property which has a numeric name in that way.

Instead you'd need to use: $object->{'23'}[0]->id


@ blocblue - Ah yes thanks, good catch, I don't run into numeric object names often completely missed that.


OK, remake the object - something like this:

$subobj = (object) array("id"=>123445566, "name"=>"Sharer Name", "offset"=>0, "length"=>14, "type"=> 'user');
$subobj2 = (object) array("id"=>123456778, "name"=>"Page Name", "offset"=>23, "length"=>14, "type"=> 'page');
$obj = (object)array( 0=>array($subobj), 23=>array($subobj2));

And test.
I couldn't get either solution to work. Perhaps I'm missing something.

Edited by diafol


// if obj is the containing object from my previous code:

$arrayObj = (array) $obj;
$scalarValue = (object) $arrayObj[23][0]->name;
echo $scalarValue->scalar;

Works, but it's really horrible. I'm an oop noob - dunnit show?!


It's probably worth noting however {'23'} won't work in PHP 5 it was depreciated, so if you're using PHP 5 you'll need a work around


Seems $object->{'23'} will work, but only if the index is a string not an integer.


$object_1 = new stdClass;
$object_1->id = 12345678;
$object_1->name = 'Sharer Name';
$object_1->offset = 0;
$object_1->length = 14;
$object_1->type = 'user';

$object_2 = new stdClass;
$object_2->id = 87654321;
$object_2->name = 'Page Name';
$object_2->offset = 23;
$object_2->length = 14;
$object_2->type = 'page';

$object = new stdClass;
$object->{0} = $object_1;
$object->{23} = $object_2;

echo '<pre>'; var_dump($object->{'23'}->{'id'}); echo '</pre>';

@diafol - your idea of casting the object to an array seems the most likely to succeed if the OP is unable to change the structure of the object.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.