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. Quote 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; Quote 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 Quote 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']; Quote 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 Quote 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. Quote 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. Quote 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" } } Quote 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; Quote 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. Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.