Created
September 14, 2012 13:11
-
-
Save simonda86/3721826 to your computer and use it in GitHub Desktop.
Insert to Array at position
This file contains hidden or 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
function array_insert($array, $var, $position) | |
{ | |
$before = array_slice($array, 0, $position); | |
$after = array_slice($array, $position); | |
$return = array_merge($before, (array) $var); | |
return array_merge($return, $after); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One code line:
array_splice($array, $position, 0, array($var));