Thursday, 2 February 2012

Drupal: iterating array items that are not properties (hashes)

I recently needed to iterate over the elements of an array, ignoring any "properties" (signified in Drupal using hashes e.g. $form['items']['#value']). Originally I did this by iterating over all the elements, and then filtering out those that I didn't want. Turns out there is a neater way: element_children(). This does the filtering for you, and returns the keys of normal (non-property) elements. You can use it like this:

foreach (element_children($form["items"]) as $i) {
    // something with $form["items"][$i]
}

1 comment:

  1. I feel your blog really useful and inspiring me. Looking at a CMS is very important I love Drupal. Because After all the influential education websites use Durpal, it's a no brainer For my Drupal hosting I always use Pantheon with fast cache responses What software did you chose?


    Drupal Managed Hosting

    ReplyDelete