1

I am not sure if it's possible or not but I am trying to pass a random variable to fill an array.

Here is the code normally:-

//Loading all data of user in an array variable.
$user_fields = user_load($user->uid);
// Updated one array variable in this array.
$user_fields -> field_module_3_status['und'][0]['value'] = "Started";
// Saved back the updated user data
user_save($user_fields);

But I want to provide the variable field_module_3_status dynamically through a variable.

Hence suppose I have $user_field_name = field_module_3_status.

So what I tried to do is:-

$user_fields = user_load($user->uid);

$this_users_status = $user_fields -> $user_field_name;

$this_users_status['und'][0]['value'] = "Started";

user_save($user_fields);

Unfortunately this doesn't work.

Any idea how I can achieve this?

2
  • 1
    $val; $ref =& $val; $val = 0; /*ref is 0 now*/ $val = 't'; /*ref is 't' now*/ this could help you along your path. EDIT: reversed &= Commented Nov 20, 2015 at 0:16
  • 1
    Thanks a lot. I tried something like this and it worked perfectly. Commented Nov 20, 2015 at 0:27

1 Answer 1

1

You're making a copy of the array when you do the assignment to $this_users_status. You need to assign to the array in the object property.

$user_fields->{$user_field_name}['und'][0]['value'] = "Started";

Or you could use a reference:

$this_users_status =& $user_fields->$user_field_name;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.