Skip to content

Instantly share code, notes, and snippets.

@Langmans
Created April 29, 2015 11:46
Show Gist options
  • Save Langmans/11929a3f2e90c1a60954 to your computer and use it in GitHub Desktop.
Save Langmans/11929a3f2e90c1a60954 to your computer and use it in GitHub Desktop.
array insert php funciton (insert indexed or associative array into another)
<?php
/**
* @param array $array
* @param string|integer $position numeric key
* @param array $insert_array
* @param string $at
*/
function array_insert(array &$array, $position, array $insert_array, $at = 'after')
{
if (!ctype_digit($position)) {
$position = array_search($position, array_keys($array));
if (false === $position) {
$array = $insert_array + $array;
return;
} else {
if ($at == 'after') {
$position++;
}
}
}
$first_array = array_splice($array, 0, $position);
$array = array_merge($first_array, $insert_array, $array);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment