Removing an element from a PHP array by value: under the hood

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

PHP array delete by value (not key): looking under the hood

\n\n

Imagine: you're writing a PHP script, processing a list of products, and suddenly you need to remove an item with a specific price or name from the array. You know the value exactly — for example, "apple" — but you have no idea what index it's at. Sound familiar? If so, you're not alone: the question "PHP array delete by value (not key)" gathers thousands of views on Stack Overflow. And that's no coincidence.

\n\n

PHP doesn't have a built-in array_remove_by_value() function. So developers improvise: some write a loop, some use array_search paired with unset, some use array_diff or array_filter. But what happens inside? Which method is faster? And why can the same code behave differently in different PHP versions?

\n\n

In this article, we won't just give you a ready-made solution — we'll look under the hood of each approach, analyze its performance and pitfalls. We'll also show a real-world case from a backend developer's life. Let's go!

\n\n

The classic way: array_search + unset

\n\n

The most popular answer on Stack Overflow looks like this:

\n\n
<?php\n$array = ['apple', 'pear', 'orange', 'pear'];\n$value = 'pear';\n\nif (($key = array_search($value, $array)) !== false) {\n    unset($array[$key]);\n}\n?>
\n\n

This code finds the first occurrence of the value and removes it. But let's break down what's actually happening here.

\n\n

How array_search works

\n\n

array_search() is a linear search. PHP goes through the array from start to finish, comparing each value with the search value. The default comparison is loose (==), meaning 1 and '1' are considered equal. If you need strict comparison (===), pass the third parameter true:

\n\n
<?php\n$array = [0, '0', 1];\n$key = array_search('0', $array, true); // returns 1 (the string '0'), not 0\n?>
\n\n

If the value is not found, the function returns false. Therefore, the check !== false is mandatory — otherwise unset($array[0]) would delete the first element if the value happens to have key 0.

\n\n

The pitfall: unset does not re-index the array

\n\n

After unset, the array remains associative — numeric keys are not shifted. If you need a dense array with sequential indices, use array_values():

\n\n
<?php\n$array = ['apple', 'pear', 'orange'];\nunset($array[1]);\n$array = array_values($array); // ['apple', 'orange'] with keys 0 and 1\n?>
\n\n

But remember: array_values() creates a new array, which increases memory consumption.

\n\n

Removing all occurrences: array_diff

\n\n

If you need to remove all elements with a given value, using array_search in a loop is not the best idea. array_diff() works much more elegantly:

\n\n
<?php\n$array = ['apple', 'pear', 'orange', 'pear'];\n$array = array_diff($array, ['pear']);\n// Result: ['apple', 'orange'] (keys are preserved)\n?>
\n\n

The function returns a new array from which all elements matching the values in the second array have been removed. Internally, PHP uses a hash table for the second array, so the operation runs in O(n + m), where n is the size of the first array and m is the size of the second.

\n\n

Important: array_diff compares values as strings ((string) $elem1 === (string) $elem2). For mixed types, this can produce unexpected results:

\n\n
<?php\n$array = [0, '0', false];\n$result = array_diff($array, [0]);\n// Result:                

Blogs

Book Recommendations