www.webdeveloper.com
Results 1 to 4 of 4

Thread: Create object property name dynamically

  1. #1
    Join Date
    Aug 2010
    Location
    Nashville
    Posts
    10

    Create object property name dynamically

    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.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,432
    Not sure if this will work (or is what you actually want):
    PHP Code:
    $profile->{$key}[0]['value'] = $value
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2010
    Location
    Nashville
    Posts
    10

    @NotDog

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

    That worked. It was exactly what I was looking for. Not sure why I could not find this in my searches. I'll look up wrapping the var in curly braces.

    Thanks.
    Dave.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,432
    Not sure where in the manual that is, but in general it's referred to as "complex variable notation", and is used when you need to give a "hint" to the parser as to where a given variable name begins and ends.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles