Page 1 of 1

object property not accessable as the array it is

Posted: Thu Jul 22, 2010 9:09 am
by mlecho
hi -
i have a funny one here. I am trying to access an array property of an object. However to find that the object/ property are what i need i run a test that the object has the wanted property...if it does, then i need to access that property (always an array) and get the value:

Code: Select all

	
        $roles  = $user->roles;
	foreach($roles as $rolekey=>$rolevalue)
	{
		//if the current object has the equivilant of this, : $node->field_california, then we will try to access it's nested array: node->field_california[0]['value']
		$field = "field_".$rolevalue;
		if(count($node->$field)>0)//we have the correct property
		{
			echo "<pre>Debug:" . print_r($node->$field[0]['value'],true) . "</pre>";
		}
			
	}
all works up to that last part where we actually print the value....if i do print_r($node->$field), i get the nested array and it's value printed...but when i try to access ([0]), all breaks. Anyone see what maybe happening here? is PHP seeing that property as a string now, and not nested array?

Re: object property not accessable as the array it is

Posted: Thu Jul 22, 2010 10:57 am
by AbraCadaver
There might be an easier way to accomplish this, but to solve your issue:

Code: Select all

if(count($node->{$field})>0)//we have the correct property
{
   echo "<pre>Debug:" . print_r($node->{$field}[0]['value'],true) . "</pre>";
}

Re: object property not accessable as the array it is

Posted: Thu Jul 22, 2010 11:35 am
by mlecho
yup...this is the answer i needed. Thanks