Search Results
array_unshift () prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won't be changed. Note: Resets array's internal pointer to the first element.
- Overview
- Syntax
- Description
- Examples
- Browser compatibility
- See also
The unshift() method of Array instances adds the specified elements to the beginning of an array and returns the new length of the array.
Parameters
element1, …, elementN The elements to add to the front of the arr.
Return value
The new length property of the object upon which the method was called.
The unshift() method inserts the given values to the beginning of an array-like object.
Array.prototype.push() has similar behavior to unshift(), but applied to the end of an array.
Please note that, if multiple elements are passed as parameters, they're inserted in chunk at the beginning of the object, in the exact same order they were passed as parameters. Hence, calling unshift() with n arguments once, or calling it n times with 1 argument (with a loop, for example), don't yield the same results.
See example:
Using unshift() Calling unshift() on non-array objects
The unshift() method reads the length property of this. It shifts all indices in the range 0 to length - 1 right by the number of arguments (incrementing their values by this number). Then, it sets each index starting at 0 with the arguments passed to unshift(). Finally, it sets the length to the previous length plus the number of prepended elements.
BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.
•Polyfill of Array.prototype.unshift in core-js with fixes of this method
•Indexed collections guide
•Array
•Array.prototype.push()
•Array.prototype.pop()
•Array.prototype.shift()
Jun 20, 2023 · 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.
Definition and Usage The array_unshift () function inserts new elements to an array. The new array values will be inserted in the beginning of the array. Tip: You can add one value, or as many as you like. Note: Numeric keys will start at 0 and increase by 1. String keys will remain the same.
Mar 13, 2025 · PHP array_unshift function tutorial shows how to add elements to the beginning of an array in PHP. Learn array_unshift with examples.
Definition and Usage The array_unshift () function inserts new elements to an array. The new array values will be inserted in the beginning of the array.
People also ask
What is array_unshift in PHP?
What does array_unshift do?
What happens if a non-key array is passed to array_unshift()?
What is int array_unshift?
La fonction array_unshift () est une fonction intégrée de PHP utilisée pour ajouter un ou plusieurs éléments au début d'un tableau. Cette fonction ne remplace pas les éléments existants du tableau, mais les déplace vers une position d'index supérieure afin d'accommoder les nouveaux éléments.
