PHP array_unshift() Function
Last Updated :
20 Jun, 2023
This inbuilt function of PHP is used to add one 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 the 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 into 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
// 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";
// Calling array_unshift()
array_unshift($array, $a1, $a2, $a3);
// Print modified array
print_r($array);
?>
Output:
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 an 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.
The below program illustrates this:
PHP
<?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 array
print_r($array);
?>
Output:
Array
(
[0] => rohan
[1] => rajeeb
[2] => saniya
[3] => ram
[4] => krishna
[5] => aakash
)
Reference: http://php.net/manual/en/function.array-unshift.php
Similar Reads
PHP array_chunk() Function The array_chunk() function is an inbuilt function in PHP which is used to split an array into parts or chunks of given size depending upon the parameters passed to the function. The last chunk may contain fewer elements than the desired size of the chunk. Syntaxarray array_chunk( $array, $size, $pre
2 min read
PHP array_combine() Function The array_combine() function is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values. That is all elements of one array will be the keys of new array and all elements of the second array will be the values of t
2 min read
PHP array_count_values() Function The array_count_values() is an inbuilt function in PHP which is used to count all the values inside an array. In other words we can say that array_count_values() function is used to calculate the frequency of all of the elements of an array. Syntax: array array_count_values( $array ) Parameters: Thi
2 min read
PHP array_diff_assoc() Function This inbuilt function of PHP is used to get the difference between one or more arrays. This function compares both the keys and values between one or more arrays and returns the difference between them. So, the function generally compares two arrays according to there keys and values and returns the
2 min read
PHP array_diff_key() Function This inbuilt function of PHP is used to get the difference between one or more arrays. This function compares the keys between one or more arrays and returns the difference between them. So, the function generally compares two arrays according to their keys and returns the elements that are present
2 min read
PHP array_diff_uassoc() Function The array_diff_uassoc() function is a built-in function in PHP and is used to get the difference between one or more arrays using an user-defined function to compare the keys. This function compares both the keys and values between one or more arrays to and returns the elements from the first array
4 min read
PHP array_diff_ukey() Function The array_diff_ukey() function is an inbuilt function in PHP. It is used to compares the key two or more arrays using a user-defined function, and return an array, which is array1 and its not present any others array2, array3 or more... Syntax: array_diff_ukey($array1, $array2, $array3..., arr_diffu
4 min read
PHP array_diff() function The array_diff() is an inbuilt function in PHP ans is used to calculate the difference between two or more arrays. This function computes difference according to the values of the elements, between one or more array and return differences in the form of a new array. This function basically returns a
2 min read
PHP array_fill_keys() Function The array_fill_keys() function is a builtin function in PHP and is used to create a new array filled with the given keys and value provided as an array to the function. Syntax: array array_fill_keys ( $keys, $value ) Parameters: This function accepts two parameters, keys and their values to be prese
3 min read
PHP array_fill() function The array_fill() is an inbuilt-function in PHP and is used to fill an array with values. This function basically creates an user-defined array with a given pre-filled value. Syntax: array_fill($start_index, $number_elements, $values) Parameter: The array_fill() function takes three parameters and ar
2 min read