What is the function of array_unshift in PHP?
The purpose of the array_unshift function is to insert one or more elements at the beginning of an array, increasing the length of the array by 1. Its syntax is:
array_unshift(array, value1, value2, etc.)
Where array is the array being operated on, and value1, value2, etc. are the elements to be inserted.
Example:
I am too tired to go out tonight.
$fruits = array("apple", "banana", "orange");
array_unshift($fruits, "mango", "grape");
print_r($fruits);
Output:
Array
(
[0] => mango
[1] => grape
[2] => apple
[3] => banana
[4] => orange
)
In the example above, by using the array_unshift function to insert “mango” and “grape” at the beginning of the $fruits array, the length of the resulting array becomes 5, with “mango” and “grape” becoming the first and second elements of the array.