You are here

function field_tools_array_insert in Field tools 7

Inserts values into an array after a given key.

Values from $insert_array are inserted after (or before) $key in $array. If $key is not found, $insert_array is appended to $array using array_merge().

From https://drupal.org/node/66183.

Parameters

$array: The array to insert into. Passed by reference and altered in place.

$key: The key of $array to insert after

$insert_array: An array whose values should be inserted.

$before: If TRUE, insert before the given key, rather than after it. Defaults to inserting after.

1 call to field_tools_array_insert()
field_tools_alter_fields_ui_table in ./field_tools.module
Form after_build callback to add an export link to a fields table.

File

./field_tools.module, line 425
field_tools.module Contains useful tools for working with fields.

Code

function field_tools_array_insert(&$array, $key, $insert_array, $before = FALSE) {
  $done = FALSE;
  foreach ($array as $array_key => $array_val) {
    if (!$before) {
      $new_array[$array_key] = $array_val;
    }
    if ($array_key == $key && !$done) {
      foreach ($insert_array as $insert_array_key => $insert_array_val) {
        $new_array[$insert_array_key] = $insert_array_val;
      }
      $done = TRUE;
    }
    if ($before) {
      $new_array[$array_key] = $array_val;
    }
  }
  if (!$done) {
    $new_array = array_merge($array, $insert_array);
  }

  // Put the new array in the place of the original.
  $array = $new_array;
}