Created
April 29, 2015 11:46
-
-
Save Langmans/11929a3f2e90c1a60954 to your computer and use it in GitHub Desktop.
array insert php funciton (insert indexed or associative array into another)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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