PHP | array_unshift() Function
This inbuilt function of PHP is used to add on or more elements into an array and these elements are added to at the beginning of the array. All the elements that we add into the array are inserted in the same order, as they have been passed. They are numerically indexed starting from 0th position. If there are string keys, then they remain unchanged.
Syntax:
int array_unshift($array, $val1, $val2, $val3....)
Parameters:
The function can take multiple parameters, depending on the number of elements we want to insert into the array. We have basically classified the parameters in two categories as explained below:
- $array: This is a mandatory parameter and refers to the original array we want to operate upon.
- List_of_values: This is a group of parameters and represents a list of values we need to insert in the array, $array. In the above syntax the List_of_values is $val1, $val2, $val3…..
Return Value: This function returns the total number of elements in the new modified array after inserting elements.
Examples:
Input : $array = ("ram", "krishna", "aakash")
$val1 = "rohan", $val2 = "rajeeb", $val3 = "saniya"
Output :
Array
(
[0] => rohan
[1] => rajeeb
[2] => saniya
[3] => ram
[4] => krishna
[5] => aakash
)
Input : $array = (1=>"ram", 2=>"krishna", 3=>"aakash")
$val1 = "rohan", $val2 = "rajeeb", $val3 = "saniya";
Output :
Array
(
[0] => rohan
[1] => rajeeb
[2] => saniya
[3] => ram
[4] => krishna
[5] => aakash
)
Below programs illustrate the array_unshift() function in PHP:
-
In this program, we will try to understand the working of the function array_unshift() by adding the elements to the beginning of the array. We will also observe that the numeric keys are added automatically.
<?php// PHP program to illustrate// the use of array_unshift()// Input Array$array=array("ram","krishna","aakash");// Values to be added$a1="rohan";$a2="rajeeb";$a3="saniya";// Callig array_unshift()array_unshift($array,$a1,$a2,$a3);// Print modified arrayprint_r($array);?>chevron_rightfilter_noneOutput:
Array ( [0] => rohan [1] => rajeeb [2] => saniya [3] => ram [4] => krishna [5] => aakash ) -
In the above program we have seen that if a non-key array is passed to the array_unshift() function then it is automatically modified to array with numeric keys. But if the array already had numeric keys starting from zero then after inserting new elements the keys will get modified. Below program illustrate this:
<?php// PHP program to illustrate// the use of array_unshift()// Input Array$array=array(1=>"ram", 2=>"krishna", 3=>"aakash");// Values to be inserted$a1="rohan";$a2="rajeeb";$a3="saniya";// Calling array_unshift()array_unshift($array,$a1,$a2,$a3);// Print modified arrayprint_r($array);?>chevron_rightfilter_noneOutput:
Array ( [0] => rohan [1] => rajeeb [2] => saniya [3] => ram [4] => krishna [5] => aakash )
Reference:
http://php.net/manual/en/function.array-unshift.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- D3.js | d3.map.set() Function
- PHP | max( ) Function
- PHP | exp() Function
- D3.js | d3.min() function
- PHP | Ds\Map get() Function
- CSS | rgb() Function
- PHP | min( ) Function
- p5.js | box() Function
- p5.js | value() Function
- D3.js | d3.rgb() Function
- p5.js | int() function
- D3.js | d3.hcl() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai



