Drupal 7 update field programatically and generate field names I'm here still learning with drupal 7 but now I have a new question about a node create programmatically,I hope you can help me.

I want to generate all node fields names programmatically and then set a lavue on each one.

To store each field from node I generate the names of the field witn an array with each name, this is because I want to make  function to indicate the names of the fields of the node that I want to fill.

The problem is that my code is not working to fill the nodes, maybe because my fields names are bad generated. I generate and fill them this way:

    $nodefields = array('areaid', 'areaname');

    foreach ($nodefields as $fieldname) {
            $node-&gt;$fieldname['und'][0]['value'] = $row-&gt;$fieldname;
            }
    node_save($node);


The rows are obtained by a query to a external db (column has the same name as the field), that's fine, the records are ok, if I fill them manually like:

$node-&gt;areaname['und'][0]['value'] = $row-&gt;$areaname;


Works ok, but I can't make it work with a loop to generalize that part.
Your problem is that below code does not work as you probably expect. 

$node-&gt;$fieldname['und'][0]['value'] = $row-&gt;$fieldname;


You expect this to work like this:

$fieldname = 'name'; 
$node-&gt;name['und'][0]['value'] = $row-&gt;name;


but PHP will probably do something like this:

$fieldname = 'name'; 
$fieldname['und'][0]['value'] = $row-&gt;name;
$node-&gt;$row-&gt;name;


Instead, try to wrap your variable name in curly brackets to have it replaced first:

$node-&gt;{$fieldname}['und'][0]['value'] = $row-&gt;$fieldname;