Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

jcnevado commented Jul 15, 2013

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