Arrays in PHP is a type of data structure that allows us to store multiple elements of similar data type under a single variable thereby saving us the effort of creating a different variable for every data. The arrays are helpful to create a list of elements of similar types, which can be accessed using their index or key.
There are two methods to insert an item at the beginning of an array which are discussed below:
Using array_merge() Function: The array_merge() function is used to merge two or more arrays into a single array. This function is used to merge the elements or values of two or more arrays together into a single array.
- Create an array containing array elements.
- Create another array containing one element which needs to insert at the beginning of another array.
- Use array_merge() function to merge both arrays to create a single array.
Array (  => GeeksforGeeks  => Computer  => Science  => Portal  => Welcome )
Using array_unshift() function: The array_unshift() function is used to add on or more elements at the beginning of the array.
Array (  => Welcome  => GeeksforGeeks  => Computer  => Science  => Portal )
Array (  => Welcome [p] => GeeksforGeeks [q] => Computer [r] => Science [s] => Portal )
- PHP program to add item at the beginning of associative array
- Program to Insert new item in array on any position in PHP
- What is the Efficient way to insert a number into a sorted array of numbers?
- Beginning BootStrap (Part-2) | Grid System
- How to remove white spaces only beginning/end of a string using PHP ?
- HTML | DOM item() Method
- PHP | DOMNamedNodeMap item() function
- HTMLCollection item() Method
- PHP | DOMNodeList item() 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 email@example.com. 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.