Merge mutiple fields into one
Last updated on
30 April 2025
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.
// 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
// 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);
Help improve this page
Page status: Not set
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion