Reputation: 122490
If I have an object with an array as an attribute, what is the easiest way to access it?
$obj->odp = array("ftw", "pwn", array("cool" => 1337));
//access "ftw"
$obj->odp->0
//access 1337
$obj->odp->2->cool
This doesn't seem to work. Is there something I'm doing wrong, or do I have to first assign it to a variable?
$arr = $obj->odp;
//access "ftw"
$arr[0]
//access 1337
$arr[2]["cool"]
Upvotes: 0
Views: 8734
Reputation: 655349
Arrays can only be accessed with the array syntax ($array['key']
) and objects only with the object syntax ($object->property
).
Use the object syntax only for objects and the array syntax only for arrays:
$obj->odp[0]
$obj->odp[2]['cool']
Upvotes: 4
Reputation: 27894
$obj->odp
is an array, so $obj->odp[0]
reads "ftw". There's no such thing like $obj->odp->0
.
Upvotes: 0