ballhogjoni Posted June 11, 2012 Share Posted June 11, 2012 I have the following code: $user->attributes()["username"] $user->attributes() is an array of the users attributes. array('username' => 'value') when i run this code: $user->attributes()["username"] i get an error: Parse error: parse error in ... What am i doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/263977-trying-to-access-an-arrays-values-but-cant/ Share on other sites More sharing options...
ignace Posted June 11, 2012 Share Posted June 11, 2012 $user->attributes['username'] Quote Link to comment https://forums.phpfreaks.com/topic/263977-trying-to-access-an-arrays-values-but-cant/#findComment-1352851 Share on other sites More sharing options...
ballhogjoni Posted June 11, 2012 Author Share Posted June 11, 2012 $user->attributes['username'] i tried that and i get Fatal error: Uncaught exception 'ActiveRecord\UndefinedPropertyException' with message 'Undefined property: User->attributes in ... Quote Link to comment https://forums.phpfreaks.com/topic/263977-trying-to-access-an-arrays-values-but-cant/#findComment-1352852 Share on other sites More sharing options...
manohoo Posted June 11, 2012 Share Posted June 11, 2012 $user->attributes() is a method (function) $user->attributes is a property (variable) if $user->attributes is an array then you can display it's contents like this: print_r($user->attributes); Quote Link to comment https://forums.phpfreaks.com/topic/263977-trying-to-access-an-arrays-values-but-cant/#findComment-1352853 Share on other sites More sharing options...
ignace Posted June 11, 2012 Share Posted June 11, 2012 If it is not php 5.4 then you can't use array dereferencing. Store it in a variable and acccess it like that. Quote Link to comment https://forums.phpfreaks.com/topic/263977-trying-to-access-an-arrays-values-but-cant/#findComment-1352855 Share on other sites More sharing options...
ManiacDan Posted June 11, 2012 Share Posted June 11, 2012 The problem is that you cannot chain array access in the form of [2] after a function call (which is fixed in a future version of PHP, as noted). The easiest way to do this is to simply make a getAttribute function on that class which would do $obj->getAttribute("username"); Quote Link to comment https://forums.phpfreaks.com/topic/263977-trying-to-access-an-arrays-values-but-cant/#findComment-1352916 Share on other sites More sharing options...
ballhogjoni Posted June 11, 2012 Author Share Posted June 11, 2012 If it is not php 5.4 then you can't use array dereferencing. Store it in a variable and acccess it like that. Ok thanks this is what I didn't want to do ... running php 5.3 ... looks like i don't have a choice unless i upgrade. The problem is that you cannot chain array access in the form of [2] after a function call (which is fixed in a future version of PHP, as noted). The easiest way to do this is to simply make a getAttribute function on that class which would do $obj->getAttribute("username"); Ok thanks! I will do this, easiest way to handle it. Don't want to have to keep making variables every time I want to access the array. Quote Link to comment https://forums.phpfreaks.com/topic/263977-trying-to-access-an-arrays-values-but-cant/#findComment-1352954 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.