Forum Moderators: coopster

Message Too Old, No Replies

Accessing objects in arrays

should be simple for you uber men!

         

bleak26

10:32 am on Feb 22, 2006 (gmt 0)

10+ Year Member



Is it possible to access the properties of an object when it is stored inside an array?

sort of like this...
echo array[object->property]

Once again, thank you.

coopster

4:01 pm on Feb 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If you convert an object to an array [php.net], you get the properties (member variables) of that object as the array's elements. The keys are the member variable names.

If you have stored just the object property, as it seems you have here, then the array index is likely numeric and the value of that index will be the value of the property object.

You can always dump the array to see how you stored it.

<?php 
print '<pre>';
print_r($array);
var_dump($array);
print '</pre>';
?>

print_r [php.net] and var_dump [php.net] with both show the structures, I just find var_dump() to be a bit more helpful when working with objects.

lobo235

4:05 pm on Feb 22, 2006 (gmt 0)

10+ Year Member



Let's say you have some "User" objects in your array and you want to access the "isActive()" method of the User object. This is the way it is done:

$arr[3]->isActive()

3 is the index of the array where the specific User object is stored.