linear Posted November 26, 2009 Share Posted November 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183059-a-variable-as-an-array-key/ Share on other sites More sharing options...
Andy-H Posted November 26, 2009 Share Posted November 26, 2009 Shouldn't it be eval("\$ref = \"$memb_node->$field\";"); $ref[0]['value'] = 123; ?? Quote Link to comment https://forums.phpfreaks.com/topic/183059-a-variable-as-an-array-key/#findComment-966137 Share on other sites More sharing options...
mikesta707 Posted November 26, 2009 Share Posted November 26, 2009 http://php.net/manual/en/language.variables.variable.php there is an ambiguity when using variable variables with arrays. try $memb_node->${$field}[0]['value'] = 123; Quote Link to comment https://forums.phpfreaks.com/topic/183059-a-variable-as-an-array-key/#findComment-966144 Share on other sites More sharing options...
linear Posted November 26, 2009 Author Share Posted November 26, 2009 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" Quote Link to comment https://forums.phpfreaks.com/topic/183059-a-variable-as-an-array-key/#findComment-966152 Share on other sites More sharing options...
Andy-H Posted November 26, 2009 Share Posted November 26, 2009 <?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) } }} Quote Link to comment https://forums.phpfreaks.com/topic/183059-a-variable-as-an-array-key/#findComment-966159 Share on other sites More sharing options...
linear Posted November 26, 2009 Author Share Posted November 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183059-a-variable-as-an-array-key/#findComment-966163 Share on other sites More sharing options...
Andy-H Posted November 26, 2009 Share Posted November 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183059-a-variable-as-an-array-key/#findComment-966170 Share on other sites More sharing options...
salathe Posted November 26, 2009 Share Posted November 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183059-a-variable-as-an-array-key/#findComment-966190 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.