function 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.


$elements: The element array whose children are to be identified.

$sort: Boolean to indicate whether the children should be sorted by weight.

Return value

The array keys of the element's children.

68 calls to element_children()
book_admin_edit_submit in drupal/modules/book/
Form submission handler for book_admin_edit().
dashboard_form_block_admin_configure_alter in drupal/modules/dashboard/dashboard.module
Implements hook_form_FORM_ID_alter().
dashboard_form_block_admin_display_form_alter in drupal/modules/dashboard/dashboard.module
Implements hook_form_FORM_ID_alter().
DrupalRenderTestCase::testDrupalRenderSorting in drupal/modules/simpletest/tests/common.test
Test sorting by weight.
drupal_pre_render_links in drupal/includes/
#pre_render callback that collects child links into a single array.

... See full list


drupal/includes/, line 6589
Common functions that many Drupal modules will need to reference.


function element_children(&$elements, $sort = FALSE) {

  // Do not attempt to sort elements which have already been sorted.
  $sort = isset($elements['#sorted']) ? !$elements['#sorted'] : $sort;

  // Filter out properties from the element, leaving only children.
  $children = array();
  $sortable = FALSE;
  foreach ($elements as $key => $value) {
    if ($key === '' || $key[0] !== '#') {
      $children[$key] = $value;
      if (is_array($value) && isset($value['#weight'])) {
        $sortable = TRUE;

  // Sort the children if necessary.
  if ($sort && $sortable) {
    uasort($children, 'element_sort');

    // Put the sorted children back into $elements in the correct order, to
    // preserve sorting if the same element is passed through
    // element_children() twice.
    foreach ($children as $key => $child) {
      $elements[$key] = $child;
    $elements['#sorted'] = TRUE;
  return array_keys($children);