protected function getGroupedOptions(array $options, array $selected) {
    $grouped_options = [];

    foreach ($options as $tid => $label) {
      // Start at the top for the current element.
      $group = &$grouped_options;

      // Find the position for this element based on its parent group. If it
      // contains a dash (-) then it's a child and we find its parent.
      while (strpos($label, '-') === 0) {
        // Remove the dash denoting this is a child.
        $label = substr($label, 1);
        // Move into the last parent added.
        end($group);
        $group = &$group[key($group)]['children'];
      }

      // This is now a new child to the currently selected parent.
      $group[$tid] = [
        'label' => $label,
        'selected' => in_array($tid, $selected),
      ];
    }

    return $grouped_options;
  }
protected function getGroupedOptions(array $options, array $selected) {
    $grouped_options = [];

    foreach ($options as $tid => $label) {
      // Start at the top for the current element.
      $group = &$grouped_options;

      // Find the position for this element based on its parent group. If it
      // contains a dash (-) then it's a child and we find its parent.
      while (strpos($label, '-') === 0) {
        // Remove the dash denoting this is a child.
        $label = substr($label, 1);
        // Move into the last parent added.
        end($group);
        $group = &$group[key($group)]['children'];
      }

      // This is now a new child to the currently selected parent.
      $group[$tid] = [
        'label' => $label,
        'selected' => in_array($tid, $selected),
      ];
    }

    return $grouped_options;
  }
protected function getGroupedOptions ( array $ options , array $ selected ) { $ grouped_options = [ ] ; foreach ( $ options as $ tid => $ label ) { // Start at the top for the current element. $ group = & $ grouped_options ; // Find the position for this element based on its parent group. If it // contains a dash (-) then it's a child and we find its parent. while ( strpos ( $ label , '-' ) === 0 ) { // Remove the dash denoting this is a child. $ label = substr ( $ label , 1 ) ; // Move into the last parent added. end ( $ group ) ; $ group = & $ group [ key ( $ group ) ] [ 'children' ] ; } // This is now a new child to the currently selected parent. $ group [ $ tid ] = [ 'label' => $ label , 'selected' => in_array ( $ tid , $ selected ) , ] ; } return $ grouped_options ; }
Group the options with their parent taxonomy terms.

This makes it easier to arrange them for display later. Relies on a proper
ordering of parent > child from the taxonomy validation.

@param array $options
The options to group.
@param array $selected
The currently selected options.

@return array
A tree structure with labels and selection status as leaf values.
Group the options with their parent taxonomy terms .