I want to insert new element after specific index of non-asociative array in PHP. This is what I've done as far:
public function insertAfter($newElement, $key)
{
// Get index of given element
$index = array_search($key, array_keys($array));
$temp = array_slice($array, $index + 1, null, TRUE);
$temp2 = array_slice($array, sizeof($array) - $index, null, TRUE);
// Insert new element into the array
$array = array_merge($temp, array($newElement), $temp2);
}
However, it doesn't really do what I want...it does some strange things with the array. Could you help?