object property not accessable as the array it is

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mlecho
Forum Commoner
Posts: 53
Joined: Wed Feb 02, 2005 9:59 am

object property not accessable as the array it is

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: object property not accessable as the array it is

Post 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>";
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
mlecho
Forum Commoner
Posts: 53
Joined: Wed Feb 02, 2005 9:59 am

Re: object property not accessable as the array it is

Post by mlecho »

yup...this is the answer i needed. Thanks
Post Reply