Skip to content

Instantly share code, notes, and snippets.

@simonda86
Created September 14, 2012 13:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonda86/3721826 to your computer and use it in GitHub Desktop.
Save simonda86/3721826 to your computer and use it in GitHub Desktop.
Insert to Array at position
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);
}
@jcnevado
Copy link

One code line:
array_splice($array, $position, 0, array($var));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment