Skip to content

Instantly share code, notes, and snippets.

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);

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.