Skip to main content

PHP Add Array Items

PHP offers several ways to put new items into an array, and the right one depends on where the items should go and how many there are:

  • [] - appends one item at the end
  • array_push() - appends one item or several at the end
  • array_unshift() - places one item or several at the start
  • array_splice() - cuts out part of an array and puts new elements in its place
  • array_merge() - combines two or more arrays into one

Add a Single Array Item

The bracket syntax [] is the quickest way to append one item to an array that already exists.

Add one item using bracket syntax

Append "Orange" to $fruits:

Prompt
Write a PHP script that creates an array with "Apple", "Banana", "Cherry", then adds "Orange" to the end using bracket [] syntax, and displays the result with var_dump().
Output: PHP script

Add multiple items one by one

Each bracket assignment adds exactly one item, so several new items take several lines:

Prompt
Write a PHP script that creates an array with "Apple", "Banana", "Cherry", then adds "Orange" and "Pear" one by one using bracket [] syntax, and displays the result with var_dump().
Output: PHP script

Associative Arrays

An associative array needs a key for the new item. Write that key inside the brackets [] and give it a value with the = operator. The item lands at the end of the array.

Add item to associative array

Give $cars a new color item:

Prompt
Write a PHP script that creates an associative array with "brand" => "Ford" and "model" => "Mustang", then adds "color" => "Red" using bracket syntax, and displays the result with var_dump().
Output: PHP script

PHP array_push() Function

When you want to append one item or several in one go, call array_push() and list them after the array.

Using array_push() to add items

Append three fruits to $fruits with a single call:

Prompt
Write a PHP script that creates an array with "Apple", "Banana", "Cherry", then uses array_push() to add "Orange", "Kiwi", and "Lemon", and displays the result with var_dump().
Output: PHP script

Add Multiple Items to Associative Arrays

The += operator lets you add several key and value pairs to an associative array in one statement.

Add multiple associative items with += operator

Extend $cars with both a color and a year:

Prompt
Write a PHP script that creates an associative array with "brand" => "Ford" and "model" => "Mustang", then adds "color" => "red" and "year" => 1964 using the += operator, and displays the result with var_dump().
Output: PHP script

PHP array_unshift() Function

array_unshift() works like array_push() but at the other end: the items you pass go in front of what the array already holds, whether that is one item or more.

Using array_unshift() to add items

Put three fruits at the front of $fruits:

Prompt
Write a PHP script that creates an array with "Apple", "Banana", "Cherry", then uses array_unshift() to add "Orange", "Kiwi", and "Lemon" to the beginning, and displays the result with var_dump().
Output: PHP script

PHP array_splice() Function

array_splice() takes a part out of an array and can put new items in its place.

Give it a position and a length of 0, so nothing gets removed, and the call simply inserts the new item at that position.

Insert item at specific position

Insert "Orange" at index 1 of $fruits, pushing the later items along:

Prompt
Write a PHP script that creates an array with "Apple", "Banana", "Cherry", then uses array_splice() to insert "Orange" at index 1 without removing any items, and displays the result with var_dump().
Output: PHP script

PHP array_merge() Function

To join two or more arrays into one, pass them to array_merge().

Merge two arrays

Combine $fruits1 and $fruits2 into a new array:

Prompt
Write a PHP script that creates two arrays ("Apple", "Banana") and ("Cherry", "Orange"), merges them using array_merge(), and displays the result with var_dump().
Output: PHP script