PHP array delete by value (not key)
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In PHP, arrays are crucial data structures that allow developers to store multiple elements under a single variable. While managing arrays, a common requirement is to delete an element based on its value rather than its key. This task, however, does not have a built-in PHP function like unset(), which works directly with keys. Thus, deleting an array element by value requires a workaround involving two primary steps: finding the key associated with the value and then removing the element using the unset() function. Here, we’ll explore how to handle array deletion by value effectively.
Understanding Array Structure in PHP
PHP arrays can be indexed or associative. Indexed arrays use numeric indexes, while associative arrays use named keys. Here's a brief example of each:
- Indexed Array:
array(0 => 'Apple', 1 => 'Banana', 2 => 'Cherry') - Associative Array:
array('first' => 'Apple', 'second' => 'Banana', 'third' => 'Cherry')
Deleting Elements by Value
To delete elements by value, you first need to determine the key(s) associated with that value. Then, you can use the unset() function to remove the element. Here is a step-by-step approach:
Step 1: Finding the Key
You can find a key using the array_search() function for a single instance or loop through the array for multiple occurrences. Here’s how you can implement both:
Single Occurrence:
Multiple Occurrences:
Step 2: Using unset() Function
Once the key(s) associated with the value are identified, the unset() function can be used to remove the element:
If there are multiple keys, as is common with duplicate values, they need to be unset within a loop.
Special Considerations
- Preserving Keys: When deleting elements, particularly in associative arrays, keys are not reindexed. If you need reindexed keys after deletion, especially in indexed arrays, use
array_values()to reset them:
- Performance: When dealing with large arrays, consider the performance implications of searching and deleting operations, especially in multiple occurrence scenarios.
- Strict Comparison:
array_search()uses loose comparison by default (==). For strict comparison (===), passtrueas the third argument toarray_search().
Summary Table
| Function | Purpose | Usages Example |
array_search() | Search array for given value and return the first corresponding key | array_search('Banana', $array) |
unset() | Remove element from array using key | unset($array[$key]) |
array_values() | Re-index numeric array keys | $array = array_values($array) |
Additional Tips
- Handling Nested Arrays: Deleting values in nested arrays requires recursive function implementation or a well-thought-out iteration mechanism.
- Alternative Methods: Libraries and frameworks often provide their mechanisms or utilities to manipulate arrays, which might be more efficient or easier than vanilla PHP code for specific scenarios.
By custom-designed functions or extensions specific to frameworks like Laravel's collections, developers can optimize and simplify array manipulations, including deleting elements by specific values.
In conclusion, though PHP does not provide a direct function to delete array elements by value, by creatively using available functions like array_search() and unset(), developers can effectively manage array data as required. SQL-like operations on arrays, including deletion by value, highlight the flexible and dynamic nature of PHP as a language suited for web development.
Related reading
- PHP Arrays - Separate Identical Values
- PHP Find All somewhat Unique Combinations of an Array
- PHP How to sort values of an array in alphabetical order?
- PHP RabbitMQ setTimeout or other option to stop waiting for queue
- Pick a random element from an array
- Picking a random element from a set
- Picking a random element from a set
- Picking unordered combinations from pools with overlap

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.