Removing an Element from an Array in PHP: A Simple Guide for Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Remove an Element from an Array in PHP: From Beginner to Pro

\n\n

Imagine: you are writing an online store in PHP. An order comes in, a product is out of stock — you need to remove it from the cart. Or you are processing a list of users, and someone decides to delete their account. In both cases, you need to remove an element from an array in PHP.

\n\n

It seems simple, right? But here a trap awaits beginners. Just calling unset() — and the array breaks: the keys are no longer sequential, and the foreach loop starts throwing surprises. Don't worry, we'll cover all the pitfalls and learn how to remove elements correctly.

\n\n

This article is an adaptation of a popular Stack Overflow question that has gathered thousands of views. We've translated complex answers into simple language and added examples from real-world practice.

\n\n

What is an Array in PHP and Why Clean It?

\n\n

An array is a container for data. Imagine a box with cells, where each cell has a number (index) or a name (key). Sometimes the data in these cells becomes outdated and needs to be discarded.

\n\n

In PHP, arrays are of two types:

\n\n
    \n
  • Indexed — keys: 0, 1, 2, 3... (like a list of products).
  • \n
  • Associative — keys: strings (e.g., "name" => "Ivan").
  • \n
\n\n

And for each type, there is its own "correct" way to delete. If you mix them up, you'll get a bug that's hard to catch.

\n\n
\n\n

Method #1: unset() — Brute Force (and Its Consequences)

\n\n

The unset($array[$key]) function is the most obvious way to remove an element from an array in PHP. It simply destroys a variable or an array element.

\n\n
<?php\n$fruits = ['apple', 'banana', 'cherry', 'date'];\nunset($fruits[1]); // remove 'banana'\nprint_r($fruits);\n?>
\n\n

Result:

\n\n
Array\n(\n    [0] => apple\n    [2] => cherry\n    [3] => date\n)
\n\n

Notice: index 1 is gone, but the other indices did not shift. Now we have an array with a gap: 0, 2, 3. If you try to iterate over it with a for loop using $i = 0; $i < count($array); $i++ — you will get an error at index 1 because it no longer exists.

\n\n

When to use unset():

\n
    \n
  • For associative arrays (keys are strings) — gaps are not a problem here.
  • \n
  • When the order of indices doesn't matter.
  • \n
\n\n

When not to use:

\n
    \n
  • For indexed arrays where sequence matters (0,1,2...).
  • \n
  • If you plan to serialize the array or pass it as JSON — gaps can break the logic on the client side.
  • \n
\n\n
\n\n

Method #2: array_splice() — Remove and Re-index

\n\n

If you want to remove an element from an array in PHP while preserving sequential numeric indices — use array_splice(). This function not only removes but also automatically "collapses" the array.

\n\n
<?php\n$fruits = ['apple', 'banana', 'cherry', 'date'];\narray_splice($fruits, 1, 1); // starting at index 1, remove 1 element\nprint_r($fruits);\n?>
\n\n

Result:

\n\n
Array\n(\n    [0] => apple\n    [1] => cherry\n    [2] => date\n)
\n\n

The indices are now 0, 1, 2 again. Beautiful! array_splice() takes three parameters:

\n
    \n
  • The array (passed by reference — the original array will be modified).
  • \n
  • The starting index.
  • \n
  • The number of elements to remove (if not specified, it removes everything to the end).
  • \n
\n\n

Bonus: this function returns the removed elements, so you can save them:

\n\n
<?php\n$removed = array_splice($fruits, 1, 1);\necho "Removed: " . $removed[0]; // banana\n?>
\n\n

Downside: only works with numeric indices. For associative arrays, <

Blogs

Book Recommendations