array_unshift(): Adding one element at starting of the array

array_unshift(): prepend values to an array

array_unshift() adds one or more values to the beginning of an array and returns the new number of elements. Numeric keys are reindexed.

$items = ['green', 'blue'];
$count = array_unshift($items, 'red');
print_r($items);
echo $count;

String keys are preserved while numeric keys are renumbered from zero.

$output=array_unshift($input);
Array unshift() to add one element at the beginning of the array
To add one or more element at the beginning of the array. After applying array_unshift() total elements of the array increases by one or more . All numerical array keys are re-indexed but literal keys remain same.

Returns the total number of elements present (after adding using array_unshift() to the array.)

Here $input is an array, $output get the value of total number of elements of the array.
$input=array('One','Two','Three','Four','Five');
$output=array_unshift($input,'added_now');
echo $output; // Output is  6
echo "<br><br>";
foreach ($input as $key => $val) {
echo "$key -> $val <br>";
}
Output of above code is here( $output gets the number of elements present 6 )
6

0 -> added_now 
1 -> One 
2 -> Two 
3 -> Three 
4 -> Four 
5 -> Five
To add more than one element we will change the code like this.
$output=array_unshift($input,'added_one','add_two');

With Literal Keys

$input=array('Fruits1' =>'Banana','Fruits2'=>'Mango','Fruits3'=>'Apple','Fruits4'=>'Grapes');
$output=array_unshift($input, 'Strawberry');
echo $output; // Output is 5
echo "<br><br>";
foreach ($input as $key => $val) {
echo "$key -> $val <br>";
}
Output is here, ( $output gets the first element Banana)
5

0 -> Strawberry 
Fruits1 -> Banana 
Fruits2 -> Mango 
Fruits3 -> Apple 
Fruits4 -> Grapes
Now key of the first element is 0. To add one element with key to an associative array we have to use the code below.
$input=array('Fruits1' =>'Banana','Fruits2'=>'Mango','Fruits3'=>'Apple','Fruits4'=>'Grapes');
$input=array('new_fruit'=> 'Strawberry') + $input;
foreach ($input as $key => $val) {
echo "$key -> $val <br>";
}
Output is here
new_fruit -> Strawberry 
Fruits1 -> Banana 
Fruits2 -> Mango 
Fruits3 -> Apple 
Fruits4 -> Grapes

Using current

By using current() we can get the element to which the present internal pointer is pointing. By defalut it points to first element of the array.
$input=array('Fruits1' =>'Banana','Fruits2'=>'Mango','Fruits3'=>'Apple','Fruits4'=>'Grapes');
echo current($input); 
echo "<br>";
$output=array_unshift($input,'Strawberry');
echo current($input); 
Output is here.( The first element is changed from Banana to Mango)
Banana
Strawberry
ARRAY REFERENCE
Array REFERENCE


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer