php

PHP array_unshift() Function

array_unshift() in PHP is used to insert the values into the array at the beginning. At a time, it is possible to append multiple values to an array. The values take key values with a numeric type that starts from 0.

Syntax:
array_unshift(array_input,value,………..)

Parameters:

  1. array_input is the array.
  2. Value/s will be added to the array at the beginning.

Let’s see examples to understand this concept better.

Example 1:
In this example, we will create an array with 4 values: ‘lotus’,’lilly’,’jasmine’,’rose’.
Now, we will add a value ‘marigold’ to the array.

<?php

//create an array named Flower1 with 4 values.
$Flower=array('lotus','lilly','jasmine','rose');

echo "Actual Array: ";
print_r($Flower);

//insert 'marigold' to the Flower array.
array_unshift($Flower,'marigold');
echo "Final Array: ";
print_r($Flower);

?>

Output:

We can see that marigold has been added to the Flower array at the beginning and key-0 is assigned to it.

Example 2:
In this example, we will create an array with four values – ‘lotus’,’lilly’,’jasmine’,’rose’.
Now, we will add three values-‘marigold’,’rose’,’lotus’ to the array.

<?php

//create an array named Flower1 with 4 values.
$Flower=array('lotus','lilly','jasmine','rose');

echo "Actual Array: ";
print_r($Flower);

//insert ‘marigold’,’rose’,’lotus’ to the Flower array.
array_unshift($Flower,'marigold','rose','lotus');
echo "Final Array: ";
print_r($Flower);

?>

Output:

We can see that ‘marigold’,’rose’,’lotus’ have been added to the Flower array at the beginning.
If you want to add values to the key-value pair array, then the values will also get the keys with numeric values.

Example 3:
In this example, we will create an array with four key-values. ‘flower1’=>’lotus’,’flower2’=>’lilly’,’flower3’=>’jasmine’,’flower4’=>’rose’.
Now, we will add 3 values-‘marigold’,’rose’,’lotus’ to the array.

<?php

//create an array named Flower1 with 4 key-values.
$Flower=array('flower1'=>'lotus','flower2'=>'lilly','flower3'=>'jasmine','flower4'=>'rose');

echo 'Actual Array: ';
print_r($Flower);

//insert ‘marigold’,’rose’,’lotus’ to the Flower array.
array_unshift($Flower,'marigold','rose','lotus');
echo 'Final Array: ';
print_r($Flower);

?>

Output:

We can see that ‘marigold’,’rose’,’lotus’ have been added to the Flower array with keys-0,1 and 2.

Example 4:
In this example, we will create an array with four key-values: 1=>’lotus’,2=>’lilly’,3=>’jasmine’,4=>’rose’.
Now, we will add 3 values-‘marigold’,’rose’,’lotus’ to the array.

<?php

//create an array named Flower1 with 4 key-values.
$Flower=array(1=>'lotus',2=>'lilly',3=>'jasmine',4=>'rose');

echo "Actual Array: ";
print_r($Flower);

//insert ‘marigold’,’rose’,’lotus’ to the Flower array.
array_unshift($Flower,'marigold','rose','lotus');
echo "Final Array: ";
print_r($Flower);

?>

Output:

We can see that ‘marigold’,’rose’,’lotus’ have been added to the Flower array with keys-5, 6, and 7 at the beginning.

Conclusion

In this article, we saw how to add elements at the beginning of the PHP array using the array_unshift() function. It is possible to add single or multiple elements to the array at a time. We have to notice that if we add values to the key-value pair array, then the newly added elements will be assigned keys of numeric type.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain