I have a regular field with multiple values, If i do

render( $fields['field1'] )

It prints


Am trying to get it to output like this


To add [ and ] to the output. I thought of using a foreach loop, but the field array is like such

'#title' => 'xxxx',
'#type' => 'xxxx',
'0' => <array>,
'1' => <array>,
'2' => <array>,

How can I get it to loop throught the [0], [1], [2] etc only?

Note: I have no control on how many multiple values the field may have, so I have to loop.

Another solution that worked well for me: Display comma-separated list of taxonomy terms?

You can make use of the power of field.tpl.php.

2 Answers 2

$field_values = element_children($fields['field1']);
foreach($field_values as $field_value) { ... }

I was looking for the answer to this too, and ended up hunting through the render() function. It wasn't too far down the chain: drupal_render() calls element_children().


Identifies the children of an element array, optionally sorted by weight.

The children of a element array are those key/value pairs whose key does not start with a '#'. See drupal_render() for details.


Why don't you try a regular expression loop after the print? See here.

If you're really experiencing problems with the loop, check out the documentation for preg_replace_callback().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.