Jump to content

trying to access an array's values, but can't.


ballhogjoni

Recommended Posts

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?

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.