3 namespace Drupal\Core\Form;
6 * Provides helpers for HTML option groups.
11 * Allows PHP array processing of multiple select options with the same value.
13 * Used for form select elements which need to validate HTML option groups
14 * and multiple options which may return the same value. Associative PHP
15 * arrays cannot handle these structures, since they share a common key.
18 * The form options array to process.
21 * An array with all hierarchical elements flattened to a single array.
23 public static function flattenOptions(array $array) {
25 static::doFlattenOptions($array, $options);
30 * Iterates over an array building a flat array with duplicate keys removed.
32 * This function also handles cases where objects are passed as array values.
35 * The form options array to process.
36 * @param array $options
37 * The array of flattened options.
39 protected static function doFlattenOptions(array $array, array &$options) {
40 foreach ($array as $key => $value) {
41 if (is_object($value) && isset($value->option)) {
42 static::doFlattenOptions($value->option, $options);
44 elseif (is_array($value)) {
45 static::doFlattenOptions($value, $options);
48 $options[$key] = $value;