Availability in `PHP 4.0`
This function appends one or more elements to the beginning of an array.
Warning: Numeric indices will be renumbered from zero. The function resets the internal pointer to the current element in the array.
array_unshift(array array, mixed var [, mixed ...])
This function takes the passed element and places it at the beginning of a new array. It then copies the original array after the passed element, preserving the keys and order.
The function returns the new number of elements in the array. However, the array is modified via reference, so it needs to be read from the variable in which it was passed.
An example usage is:
$queue = ['p1', 'p3'];array_unshift($queue, 'p4', 'p5', 'p6');
The modified array $queue
will have 5 elements: 'p4', 'p5', 'p6', 'p1' and 'p3'.
Parameter | Data type | Default value | Note |
---|---|---|---|
$array |
array |
not | Modified array. |
$var |
mixed |
not | The value that goes to the beginning of the array. |
$_ |
mixed |
null | Optional additional values that will go to the top of the array. |
Returns int
representing the new number of elements in the array.
[Official documentation for the array-unshift function](Official documentation)
Jan Barášek Více o autorovi
Autor článku pracuje jako seniorní vývojář a software architekt v Praze. Navrhuje a spravuje velké webové aplikace, které znáte a používáte. Od roku 2009 nabral bohaté zkušenosti, které tímto webem předává dál.
Rád vám pomůžu:
Články píše Jan Barášek © 2009-2024 | Kontakt | Mapa webu
Status | Aktualizováno: ... | en