Jump to content

set object property names dynamically


davedpss

Recommended Posts

This is on a drupal 6 site. I need to spin through an array and set content profile properties using the $key followed by [0]['value'] = $value; I keep getting errors and can't figure out how to create the property dynamically. Here is the code: foreach ($profile_array as $key => $value) { $profile->$key[0]['value'] = $value;} I have tried multiple variations of concatenating $key to [0]['value'] including creating variables and then inserting them in place but nothing is working. Any suggestions on what I can try would be appreciated. Thanks.Dave.

Link to comment
Share on other sites

what are the error messages? What are your data structures like? Maybe you want to do something like this?

//assuming $profile is an array $profile = array() foreach ($profile_array as $key => $value) {	  $profile[$key] = $value;}

Edited by thescientist
Link to comment
Share on other sites

$profile is an object and I need to update properties of that object and then use node_save($profile) function to update the database. The issue is that the property names all have [0]['value'] in them. For example the $key variable might be "field_first_name" and the property to update is "field_first_name[0]['value']". I'm just having trouble trying to figure how to combine the value of $key with the string "[0]['value']" to create the property name. Thanks.

Link to comment
Share on other sites

@justsomeguy, Thanks, that worked. I am curious as to why if I used the value of $key in stead of the variable e.g. $profile->field_first_name[0]['value'] = $value; that this worked. I would have thought that the arrays were created when I loaded the $profile object (earlier in the script). I have a similar script where I reference all the object properties by name instead of using a variable as part of the name and they all work??? Oh well, this fixes the issue. Thanks very much. Dave.

Link to comment
Share on other sites

yoour profile array looks like $profile_array which is different from $profile.unless $profile has prvious value. you can always us print_r() or var_dump() to see what does an array structure looks like

Link to comment
Share on other sites

$profile->{$key}[0]['value'] = $value;

The code above also worked and was more in line with what I was trying to do. I'll look up what the curly braces around the variable do.Thanks to all for their help.Dave.

Edited by Dave B.
Link to comment
Share on other sites

The curly braces tell it to use the value of the variable as the property name. It's fine to use that syntax to refer to the value after you've created it, but you should create the arrays like I showed. If you turn on maximum error reporting you should see it give a notice for trying to set the value like that without declaring the arrays first.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...