function D7field_tools_array_insert in Field tools 8
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.
File
- ./
field_tools.module, line 367 - field_tools.module Contains useful tools for working with fields.
Code
function D7field_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;
}