Merge mutiple fields into one

Last modified: December 4, 2008 - 23:36

I have 2 option cck fields, one is a predefined list, the other is a text field. Both are set to unlimited number of values. I first tried to use array_merge, since it would be a more "elegant" solution; but I couldn't make it fly. This was my first attempt.

<?php
// set temp variable
$count = 0;

// put array into computed field array pt1
foreach (element_children($node->field_option) as $key) {
   
$node_field[$count]['value'] = $node->field_option[$key]['value'];
   
$count++;
}

// put array into computed field array pt2
foreach (element_children($node->field_option_list) as $key) {
   
$node_field[$count]['value'] = $node->field_option_list[$key]['value'];
   
$count++;
}
?>



To make this into a comma separated list, here is the code. This works with "Store using the database", while the top one doesn't for some reason. This is what I'm using now
<?php
// set temp variable
$temp = array();

// put array into computed field array pt1
foreach (element_children($node->field_option) as $key) {
   
$temp[] = $node->field_option[$key]['value'];
}

// put array into computed field array pt2
foreach (element_children($node->field_option_list) as $key) {
   
$temp[] = $node->field_option_list[$key]['value'];
}

//kill empty values in the array
foreach ($temp as $key => $value) {
    if (
is_null($value) || $value=="") {
        unset(
$temp[$key]);
    }
}

//implode to comma list
$node_field[0]['value'] = implode(', ', $temp);
?>

Is there more going on here

esend7881 - April 30, 2009 - 01:52

Is there more going on here than meets the eye?

I know changing the field to your specific type (like $node->field_YOURFIELD is required, but what else is needed to make this work?

Right now, that code (both of them) don't do anything.... or at least I don't see it doing anything..

This is for combining 2

mikeytown2 - April 30, 2009 - 08:54

This is for combining 2 option fields. They both contain the same kind of data (text). Try it out with 2 option fields. I haven't tried this with mixed data types.

Just for reference for future

esend7881 - May 1, 2009 - 02:29

Just for reference for future people seeing this thread: http://drupal.org/node/449206 see here for details

 
 

Drupal is a registered trademark of Dries Buytaert.