Jump to content

Recommended Posts

Ideally, I'd like to be able to use a variable, like

    $field = 'field_one';

in the following statement

    $memb_node->$field[0]['value'] = 123;

This fails.

 

To emphasize, one might use

$memb_node->field_one[0]['value'] = 123;

which runs correctly, but I want to make the field_one part a variable,

so that I can dynamically set different fields

 

In my lame attempts, I've tried

      $ref = eval("\$memb_node->\"$field\";");

      $ref[0]['value']= 1234;

but it chokes on the first line.

Obviously I'm new at all this.  :confused:

 

 

Link to comment
https://forums.phpfreaks.com/topic/183059-a-variable-as-an-array-key/
Share on other sites

thanks, yes I misused eval, but the new suggestion gives

  "Cannot use string offset as an array"

 

The next suggestion

$memb_node->${$field}[0]['value'] = 123; 

gives

"Cannot access empty property "

 

Then I tried,

$memb_node->{$field}[0]['value'] = 222;
  

which successfully wrote the value, however I got

  "Recoverable fatal error: Object of class stdClass could not be converted  to string"

 

 

 

 

<?php

class arr
{

    public $field_one;

    public function __construct()
    {
    }

}

$field = 'field_one';

$memb_node = new arr();
$ref = &$memb_node->$field;
$ref[0]['value'] = 123;

var_dump($memb_node);

?>

 

Outputted:

 

object(arr)#1 (1) {  ["field_one"]=>  &array(1) {    [0]=>    array(1) {      ["value"]=>      int(123)    }  }}

I retried

$memb_node->{$field}[0]['value'] = 222;

and its seemed to work this time.

 

Also, latest approach kindly given,

$ref = &$memb_node->$field;
$ref[0]['value'] = 123;

works too.

 

Thanks for everyones fast replies!

If anyone would care to add a few clarifying comments to help this newbies understanding, that would be great.

Because the variable is surrounded by the braces, it is the first part of the code parsed on that line; just like brackets in math.

 

This way the $field variable is evaluated to it's value (field_one), then the rest of the code is parsed and it reads

 

$memb_node->field_one[0]['value'] = 222;

 

in the example I provided, the object property passed to $ref is passed by reference, PHP simply adds another way to access the memory(RAM) location internally and when $ref is updated, it also updates $memb_node as both variables point to the same memory location.

 

P.S

 

I would use:

$memb_node->{$field}[0]['value'] = 222;

 

that method as it seems much more simple and readable to me.

Because the variable is surrounded by the braces, it is the first part of the code parsed on that line; just like brackets in math.

The line is "parsed" from left to right as normal just in a different manner when it finds the curly braces compared to when none are present.  It's not much to explain precisely how they're done differently, but perhaps that would just add cruft to the topic (do we really want to delve into opcodes here?).  Not that this post in and of itself isn't fully crufty anyway! Sorry, don't mean to intrude. Carry on. :shy:

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.