Функция array_unshift() добавляет новые элементы
в начало массива array. Обратите внимание,
функция добавляет список элементов целиком, поэтому новые элементы остаются в то же порядке.
Числовые ключи массива изменятся так, чтобы нумерация начиналась
с нуля, тогда как литеральные ключи не изменятся.
Замечание:
Функция сбрасывает внутренний указатель массива на первый элемент.
Sahn's example almost works but has a small error. Try it like this if you need to prepend something to the array without the keys being reindexed and/or need to prepend a key value pair, you can use this short function:
It reindexes them based on the order they were created. It seems like if an array has all numeric indexes, then it should reindex them based on the order of their index. Just my opinion...
even simpler unshifting of a reference ! <?php /** * @return int * @param $array array * @param $value mixed * @desc Prepend a reference to an element to the beginning of an array. Renumbers numeric keys, so $value is always inserted to $array[0] */ function array_unshift_ref(&$array, &$value) { $return = array_unshift($array,''); $array[0] =& $value; return $return; } ?>
If you need to prepend something to the array without the keys being reindexed and/or need to prepend a key value pair, you can use this short function:
I had a need tonight to convert a numeric array from 1-based to 0-based, and found that the following worked just fine due to the "side effect" of renumbering: