Skip to main content

PHP Remove Array Items

PHP has several functions for taking items out of an array, each suited to a different situation:

  • array_splice() - takes out a stretch of items, given a start position and a length
  • unset() - deletes the element stored under a specific key
  • array_diff() - filters items out of an associative array
  • array_pop() - drops the last item
  • array_shift() - drops the first item

PHP array_splice() Function

array_splice() needs to know two things: the index to start at and the number of items to delete.

Once the items are gone, PHP renumbers what is left, so the indices run from 0 again without gaps.

Remove one item with array_splice()

Delete one item at index 1, the second car:

Prompt
Write a PHP script that creates an array with "Volvo", "BMW", "Toyota", then uses array_splice() to remove the second item, and displays the result with var_dump().
Output: PHP script

Remove Multiple Array Items

Deleting several items with array_splice() is a matter of its length parameter, which sets how many items go.

Remove multiple items with array_splice()

Delete 2 items, beginning at index 1 (the second item):

Prompt
Write a PHP script that creates an array with "Volvo", "BMW", "Toyota", then uses array_splice() to remove 2 items starting at index 1, and displays the result with var_dump().
Output: PHP script

PHP unset() Function

unset() is another way to delete items that exist in an array.

Note: Unlike array_splice(), it leaves the numbering alone. Delete the element at index 1 and the others stay at 0, 2, 3 and so on, so a gap appears in the indices.

Remove one item with unset()

Delete the second item and notice the gap in the output:

Prompt
Write a PHP script that creates an array with "Volvo", "BMW", "Toyota", then uses unset() to remove the second item, and displays the result with var_dump().
Output: PHP script

Remove Multiple Array Items with unset()

Since unset() accepts as many arguments as you like, one call can delete several items:

Remove multiple items with unset()

Delete the items at index 0 and index 1 together:

Prompt
Write a PHP script that creates an array with "Volvo", "BMW", "Toyota", then uses unset() to remove the first and second items, and displays the result with var_dump().
Output: PHP script

Remove Item From an Associative Array

unset() also works on associative arrays.

Just pass it the key of the item that should go.

Remove associative array item with unset()

Delete the "model" item:

Prompt
Write a PHP script that creates an associative array with "brand" => "Ford", "model" => "Mustang", "year" => 1964, then uses unset() to remove the "model" key, and displays the result with var_dump().
Output: PHP script

PHP array_diff() Function

Another option for associative arrays is array_diff().

It leaves the original array untouched and returns a new one that lacks the items you listed.

Note: You list values here, not keys.

Remove items with array_diff()

Build a new array that leaves out "Mustang" and "1964":

Prompt
Write a PHP script that creates an associative array with "brand" => "Ford", "model" => "Mustang", "year" => 1964, then uses array_diff() to create a new array without "Mustang" and 1964, and displays the result with var_dump().
Output: PHP script

PHP array_pop() - Remove Last Array Item

To drop the final item of an array, call array_pop().

Remove last item with array_pop()

Drop "Toyota", the last car:

Prompt
Write a PHP script that creates an array with "Volvo", "BMW", "Toyota", then uses array_pop() to remove the last item, and displays the result with var_dump().
Output: PHP script

PHP array_shift() - Remove First Array Item

To drop the item at the very start of an array, call array_shift().

Remove first item with array_shift()

Drop "Volvo", the first car:

Prompt
Write a PHP script that creates an array with "Volvo", "BMW", "Toyota", then uses array_shift() to remove the first item, and displays the result with var_dump().
Output: PHP script