Search⌘ K
AI Features

Adding Elements in an Array

Explore how to add elements to PHP arrays at the beginning using array_unshift, at the end with array_push, or at any position by assigning values to keys. This lesson helps you manipulate arrays effectively for better data management in your PHP programs.

Adding Elements at the Start #

Sometimes you want to add an element to the beginning of an array without modifying any of the current elements within the array. Whenever this is the case, you can use array_unshift() function.

array_unshift() prepends passed elements at the front of an 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 ...