BahBah Posted December 24, 2009 Share Posted December 24, 2009 Hello My object looks like this: Array ( [0] => User Object ( [id] => [username] => [password] => [title] => [firstname] => [surname] => [email] => [active] => [salt] => M}8 ) ) This is the result of a SQL method from my SQL class. $result_array = self::find_by_sql($sql); Can you please tell me how I can access the 'salt' element of the object array above ? I've been trying: $result_array->salt but that doesn't work. Thanks for any assistance. Link to comment https://forums.phpfreaks.com/topic/186299-accessing-element-of-object-array/ Share on other sites More sharing options...
rajivgonsalves Posted December 24, 2009 Share Posted December 24, 2009 this should work echo $result_array->salt; Link to comment https://forums.phpfreaks.com/topic/186299-accessing-element-of-object-array/#findComment-983851 Share on other sites More sharing options...
BahBah Posted December 24, 2009 Author Share Posted December 24, 2009 Thanks for your reply. This is the error I get when I try that: Notice: Trying to get property of non-object Link to comment https://forums.phpfreaks.com/topic/186299-accessing-element-of-object-array/#findComment-983852 Share on other sites More sharing options...
waynew Posted December 24, 2009 Share Posted December 24, 2009 What's the name of the array? echo $nameofarray[0]['salt']; Link to comment https://forums.phpfreaks.com/topic/186299-accessing-element-of-object-array/#findComment-983853 Share on other sites More sharing options...
BahBah Posted December 24, 2009 Author Share Posted December 24, 2009 Thanks for your reply. When I try $result_array[0]['salt'] I get: Fatal error: Cannot use object of type User as array Link to comment https://forums.phpfreaks.com/topic/186299-accessing-element-of-object-array/#findComment-983854 Share on other sites More sharing options...
BahBah Posted December 24, 2009 Author Share Posted December 24, 2009 I suspect this may be down to the actual method that invokes all of this is a public static function. So it doesn't have access to $this. I'm still not sure why I cannot access the array elements though. Link to comment https://forums.phpfreaks.com/topic/186299-accessing-element-of-object-array/#findComment-983857 Share on other sites More sharing options...
Buddski Posted December 24, 2009 Share Posted December 24, 2009 Can you do a var_dump on your array/object so we can actually see what we are trying to help you retrieve. Link to comment https://forums.phpfreaks.com/topic/186299-accessing-element-of-object-array/#findComment-983858 Share on other sites More sharing options...
BahBah Posted December 25, 2009 Author Share Posted December 25, 2009 Here's the var_dump: array(1) { [0]=> object(User)#4 (9) { ["id"]=> NULL ["username"]=> NULL ["password"]=> NULL ["title"]=> NULL ["firstname"]=> NULL ["surname"]=> NULL ["email"]=> NULL ["active"]=> NULL ["salt"]=> string(3) "M}8" } } Link to comment https://forums.phpfreaks.com/topic/186299-accessing-element-of-object-array/#findComment-983860 Share on other sites More sharing options...
Alex Posted December 25, 2009 Share Posted December 25, 2009 Try: echo $result_array[0]->salt; Link to comment https://forums.phpfreaks.com/topic/186299-accessing-element-of-object-array/#findComment-983861 Share on other sites More sharing options...
BahBah Posted December 25, 2009 Author Share Posted December 25, 2009 That did it. Thank you ! I appreciate your not having seen all of my classes, but is there a common reason for having to access the array in that form ? because it wasn't my intention for it to read like that. Link to comment https://forums.phpfreaks.com/topic/186299-accessing-element-of-object-array/#findComment-983862 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.