Warning: Directory /var/www/blastcoding.com/public_html/blastcoding/htmlpurifier/library/HTMLPurifier/DefinitionCache/Serializer not writable, please chmod to 777 in /var/www/blastcoding.com/public_html/blastcoding/htmlpurifier/library/HTMLPurifier/DefinitionCache/Serializer.php on line 294
Warning: Directory /var/www/blastcoding.com/public_html/blastcoding/htmlpurifier/library/HTMLPurifier/DefinitionCache/Serializer not writable, please chmod to 777 in /var/www/blastcoding.com/public_html/blastcoding/htmlpurifier/library/HTMLPurifier/DefinitionCache/Serializer.php on line 294
Warning: Directory /var/www/blastcoding.com/public_html/blastcoding/htmlpurifier/library/HTMLPurifier/DefinitionCache/Serializer not writable, please chmod to 777 in /var/www/blastcoding.com/public_html/blastcoding/htmlpurifier/library/HTMLPurifier/DefinitionCache/Serializer.php on line 294
PHP array_unshift function
The PHP function array_unshift inserts one or more elements at the beginning of the array.
Description / Descripciónarray_unshift(array &$array, mixed ...$values): intSintaxis / Sintax
$narray = array_unshift($array,$value);
Numeric arrays will be modified to start from 0.
Parameters
$array – the array to which new elements will be added to the left.
$values – the values we will add.
Returns
int – returns the number of new elements in the array.
PHP array_unshift examples
$pearson = array(
"nombre" => "Juan",
"edad" => 25,
"ciudad" => "EjemploCity"
);
array_unshift($pearson,"Paraguay");
print_r($pearson);
Array
(
[0] => Paraguay
[nombre] => Juan
[edad] => 25
[ciudad] => EjemploCity
)
Numeric array example:
usando array_unshift en un array numerico$numeros = array(10, 20, 30, 40, 50); array_unshift($numeros,1,2); print_r($numeros);
Array
(
[0] => 1
[1] => 2
[2] => 10
[3] => 20
[4] => 30
[5] => 40
[6] => 50
)
Uses
Queue Management: If you are implementing a queue of elements and need to insert new elements at the beginning of the queue so that they are processed first, array_unshift can be useful.
$queue = array(); array_unshift($queue, "Nuevo elemento");
History or Stack Handling: In situations where you are building a history or a stack of elements and want to add new elements at the top so that they are the most recent ones.
$history = array("Elemento antiguo");
array_unshift($history, "Nuevo elemento");
Normally, you would push and pop in a stack, but you could also do something like this if there are elements you want to remove from the end.

