You are here

function webform_grid_merge_options in Webform 7.4

Helper. Merge select component options in order.

Parameters

array $existing: An array of existing values into which any values from $new that aren't in $existing are inserted.

array $new: Values to be inserted into $existing.

Return value

array The merged array.

1 call to webform_grid_merge_options()
webform_expand_grid in components/grid.inc
A Form API #process function for Webform grid fields.

File

components/grid.inc, line 327
Webform module grid component.

Code

function webform_grid_merge_options(array $existing, array $new) {
  $insert = NULL;
  $queue = array();
  foreach ($new as $key => $value) {
    if (isset($existing[$key])) {

      // Insert the queue before the found item.
      $insert = array_search($key, array_keys($existing));
      if ($queue) {
        $existing = array_slice($existing, 0, $insert, TRUE) + $queue + array_slice($existing, $insert, NULL, TRUE);
        $insert += count($queue);
        $queue = array();
      }
      $insert++;
    }
    elseif (is_null($insert)) {

      // It is not yet clear yet where to put this item. Add it to the queue.
      $queue[$key] = $value;
    }
    else {

      // PHP array_splice does not preserved the keys of the inserted array,
      // but array_slice does (if the preserve keys parameter is TRUE).
      $existing = array_slice($existing, 0, $insert, TRUE) + array(
        $key => $value,
      ) + array_slice($existing, $insert, NULL, TRUE);
      $insert++;
    }
  }

  // Append any left over queued items.
  $existing += $queue;
  return $existing;
}