Since $form['submitted'] array has also other keys than just field names e.g. #tree, what is the correct way to loop over this array?

I need to add a #prefix and #suffix for each field and for now I've just created condition !is_numeric(strpos($fName,"#")) This tells if it just a field name or if it holds render array properties or whatever they are called.

  • @Pierre.Vriens Is there a reason you keep asking people if they are using Drupal 5?
    – mpdonadio
    Feb 12, 2017 at 1:51
  • @mpdonadio : yes, just to make sure that if I'd answer the question, I don't post an answer that does not help OPer. And so that I don't waste my time in preparing an answer that is at risk of being flagged as something like "not an answer". Isn't it allowed to post comments to better understand the question? Or should I rather right away vote to close such questions as "unclear what you're asking"? Or should I rather post an answer for each possible release (eg like the existing one which assumes it is D7)? Feb 12, 2017 at 7:47
  • 2
    @Pierre.Vriens Of course asking for clarification is appropriate. But, asking if someone is using Drupal 5 borders on rude and flippant, and doesn't show any real respect for the user. I also think is a passive aggressive response to the open issue of how we handle generic questions and version tags (which is the SE team's hands right now). What is also not appropriate is adding these tags back in when you get a response.
    – mpdonadio
    Feb 12, 2017 at 18:33

1 Answer 1


For D7 use 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.


foreach (element_children($form['submitted']) as $key) {
    $form['submitted'][$key]['#prefix'] = 'foo';

For D8, use the equivalent Element::children in the same manner.

  • Sounds like a solution :) I'll take it for a test drive. Feb 11, 2017 at 11:10
  • Pretty sure that element_children() pattern works all the way back to Drupal 4.7.
    – mpdonadio
    Feb 12, 2017 at 18:34

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.