Help me to understand what element_child() does Can anybody help me understand what element_child is doing?

It is called as element_children($form) where form contains keys like "cancel", "submit" and "#redirect".

This snippet from common.inc

/**
 * Check if the key is a child.
 */
function element_child($key) {
  return !isset($key[0]) || $key[0] != '#';
}

/**
 * Get keys of a structured array tree element that are not properties (i.e., do not begin with '#').
 */
function element_children($element) {
  return array_filter(array_keys((array) $element), 'element_child');
}
Suppose you have an array as the following (it's an array as accepted from the form API):

  $form['example_text'] = array(
    '#type' =&gt; 'textfield', 
    '#title' =&gt; t('Testing text'), 
    '#default_value' =&gt; '', 
    '#size' =&gt; 45, 
    '#maxlength' =&gt; 45, 
    '#required' =&gt; TRUE, 
    '#description' =&gt; t('This is an example form demonstrating forms usage in the installer profiles tasks. Enter any text to see what happens.'),
  );

  $form['continue'] = array(
    '#type' =&gt; 'submit', 
    '#value' =&gt; t('Continue'),
  );

  $form['errors'] = array();
  $form['#action'] = $url;
  $form['#redirect'] = FALSE;


If you execute element_children($form) will return an array containing the following indexes:


example_text
continue
errors


When $key is a string $key[0] is the first character of that string. The value of !isset($key[0]) || $key[0] != '#' is TRUE when the string $key is empty, or when the first character of that string is not #; array_filter() returns an array containing the keys and values for which the function referred as second argument returns TRUE.

The reason to do this is that, in the array used by the form API, keys starting with # are considered properties of that form field (or of the form), while keys that don't start with # are considered child of that form field; in the example code I used, '#redirect' is a property of the form, while 'example_text' is a form field that is child of the form.