array_unshift

(PHP 4, PHP 5)

array_unshiftPrepend one or more elements to the beginning of an array

Description

int array_unshift ( array &$array , mixed $value1 [, mixed $... ] )

array_unshift() prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won't be touched.

Parameters

array

The input array.

value1

First value to prepend.

Return Values

Returns the new number of elements in the array.

Examples

Example #1 array_unshift() example

<?php
$queue 
= array("orange""banana");
array_unshift($queue"apple""raspberry");
print_r($queue);
?>

The above example will output:

Array
(
    [0] => apple
    [1] => raspberry
    [2] => orange
    [3] => banana
)

See Also

Коментарии

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:

<?php
function array_unshift_assoc(&$arr$key$val)
{
   
$arr array_reverse($arrtrue);
   
$arr[$key] = $val;
   
$arr array_reverse($arrtrue);
    return 
count($arr);
}
?>
2001-07-27 15:21:56
http://php5.kiev.ua/manual/ru/function.array-unshift.html
Actually this problem with the keys getting reindexed only happens when the keys are numerical:

<?php

$a 
= array("f"=>"five""s" =>"six""t" =>
       
"twenty");

print_r($a);
echo 
"\n";
foreach(
$a as $key=>$val)
{
    echo 
"k: $key v: $val \n";
}

array_unshift($a"zero");
print_r($a);
echo 
"\n";
foreach(
$a as $key=>$val)
{
    echo 
"k: $key v: $val \n";
}
?>

Array
(
    [f] => five
    [s] => six
    [t] => twenty
)

k: f v: five
k: s v: six
k: t v: twenty
Array
(
    [0] => zero
    [f] => five
    [s] => six
    [t] => twenty
)

k: 0 v: zero
k: f v: five
k: s v: six
k: t v: twenty
2002-02-07 08:02:55
http://php5.kiev.ua/manual/ru/function.array-unshift.html
array_merge() will also reindex (see array_merge() manual entry), but the '+' operator won't, so...

<?php
$arrayone
=array("newkey"=>"newvalue") + $arrayone;
?>

does the job.
2002-07-30 22:00:24
http://php5.kiev.ua/manual/ru/function.array-unshift.html
If you need to change the name of a key without changing its position in the array this function may be useful.

<?php
function array_key_change($Old$New$In$NewVal=NULL) {
       
$Temp = array();
        while(isset(
$Temp[$Old]) == false) {
                list(
$k$v) = each($In);
               
$Temp[$k] = $v;
                unset(
$In[$k]);
        }
        if(
$NewVal == NULL) {
               
$NewVal $Temp[$Old];
        }
        unset(
$Temp[$Old]);
       
$Temp array_reverse($Temp);
       
$In array_merge(array($New=>$NewVal), $In);
        while(list(
$k,$v) = each($Temp)) {
               
$In array_merge(array($k=>$v), $In);
        }
        return(
$In);
}
?>
2003-07-23 15:17:59
http://php5.kiev.ua/manual/ru/function.array-unshift.html
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;
}
?>
2004-02-26 20:20:45
http://php5.kiev.ua/manual/ru/function.array-unshift.html
Автор:
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:

<?php
   array_unshift
$myArrayarray_shift$myArray ));
?>
2006-06-02 21:54:23
http://php5.kiev.ua/manual/ru/function.array-unshift.html
This becomes a nice little problem if you index your arrays out of order (while manually sorting).  For example:

<?php
$recordMonths
[3] = '8/%/2006';
$recordMonths[4] = '7/%/2004';
$recordMonths[0] = '3/%/2007';
$recordMonths[1] = '2/%/2007';
$recordMonths[5] = '12/%/2000';
$recordMonths[6] = '11/%/2000';
$recordMonths[7] = '10/%/2000';
$recordMonths[2] = '1/%/2007';

for(
$i 0$i count($recordMonths); $i++)
{
   
$singleMonth $recordMonths[$i];
    echo 
"singleMonth: $singleMonth <br />";
}
array_unshift($recordMonths,'%');
for(
$i 0$i count($recordMonths); $i++)
{
   
$singleMonth $recordMonths[$i];
    echo 
"singleMonth: $singleMonth <br />";
}
?>

Produces:

singleMonth: 3/%/2007
singleMonth: 2/%/2007
singleMonth: 1/%/2007
singleMonth: 8/%/2006
singleMonth: 7/%/2004
singleMonth: 12/%/2000
singleMonth: 11/%/2000
singleMonth: 10/%/2000
singleMonth: %
singleMonth: 8/%/2006
singleMonth: 7/%/2004
singleMonth: 3/%/2007
singleMonth: 2/%/2007
singleMonth: 12/%/2000
singleMonth: 11/%/2000
singleMonth: 10/%/2000
singleMonth: 1/%/2007 

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...
2007-03-26 12:13:05
http://php5.kiev.ua/manual/ru/function.array-unshift.html
You can preserve keys and unshift an array with numerical indexes in a really simple way if you'll do the following:

<?php
$someArray
=array(224=>'someword1'228=>'someword2'102=>'someword3'544=>'someword3',95=>'someword4');

$someArray=array(100=>'Test Element 1 ',255=>'Test Element 2')+$someArray;
?>

now the array looks as follows:

array(
100=>'Test Element 1 ',
255=>'Test Element 2'
224=>'someword1',
228=>'someword2',
102=>'someword3',
544=>'someword3',
95=>'someword4'
);
2007-10-03 23:49:46
http://php5.kiev.ua/manual/ru/function.array-unshift.html
Автор:
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: 

<?php 
function array_unshift_assoc(&$arr$key$val

   
$arr array_reverse($arrtrue); 
   
$arr[$key] = $val
    return = 
array_reverse($arrtrue); 

?>
2011-11-19 11:44:57
http://php5.kiev.ua/manual/ru/function.array-unshift.html
Anonymous' associative version wasn't working for me, but it did with this small tweak:

function array_unshift_assoc(&$arr, $key, $val) 

    $arr = array_reverse($arr, true); 
    $arr[$key] = $val; 
    $arr = array_reverse($arr, true); 
    return $arr;
}
2015-04-17 11:18:53
http://php5.kiev.ua/manual/ru/function.array-unshift.html
Автор:
Another way to tack something to the beginning of an array is with array_merge().

$plans = array('AARP'=>'Senior', 'AAA'=>'Automobile Club');

$plans = array_merge(array("BAR"=>"Best Available Rate"),  $plans);
2016-03-16 20:31:15
http://php5.kiev.ua/manual/ru/function.array-unshift.html
This function helps if you want to prepend a key and value pair to the beginning of an array:

function array_kunshift(array $array, string|int $key, mixed $value): array {
        return array_merge([$key => $value], $array);
    }
2023-11-06 22:52:46
http://php5.kiev.ua/manual/ru/function.array-unshift.html

    Поддержать сайт на родительском проекте КГБ