To delete elements from a PHP array by value (not by key), you can use several approaches depending on whether you want to remove the first occurrence, all occurrences, or create a new array without the value. Below are the methods with examples:
1. Remove the First Occurrence of a Value
Use array_search()
to find the key of the value, then unset()
it.
Note: This method preserves keys and does not reindex the array.
$array = ['a', 'b', 'c', 'b'];
$value = 'b';
// Find the key of the first occurrence
if (($key = array_search($value, $array)) !== false) {
unset($array[$key]);
}
print_r($array);
// Output: Array ( [0] => a [2] => c [3] => b )
Strict Comparison: Add true
to enforce type checking:
$key = array_search($value, $array, true);
2. Remove All Occurrences of a Value
Loop through all keys of the value using array_keys()
and unset()
each.
$array = ['a', 'b', 'c', 'b'];
$value = 'b';
// Get all keys matching the value
foreach (array_keys($array, $value) as $key) {
unset($array[$key]);
}
print_r($array);
// Output: Array ( [0] => a [2] => c )
Reindex the Array (optional):
$array = array_values($array);
// Output: Array ( [0] => a [1] => c )
3. Create a New Array Without the Value
Use array_diff()
to exclude the value(s).
Note: Returns a new array and preserves keys unless reindexed.
$array = ['a', 'b', 'c', 'b'];
$value = 'b';
// Remove all instances of $value
$newArray = array_diff($array, [$value]);
print_r($newArray);
// Output: Array ( [0] => a [2] => c )
Reindex the Array (optional):
$newArray = array_values($newArray);
// Output: Array ( [0] => a [1] => c )
4. Filter Values with array_filter()
Use a callback to exclude the value.
Note: Preserves keys unless reindexed.
$array = ['a', 'b', 'c', 'b'];
$value = 'b';
// Remove all instances of $value
$newArray = array_filter($array, function($element) use ($value) {
return $element !== $value;
});
print_r($newArray);
// Output: Array ( [0] => a [2] => c )
Reindex the Array (optional):
$newArray = array_values($newArray);
// Output: Array ( [0] => a [1] => c )
Key Considerations
Method | Use Case | Key Preservation | Reindexing Needed? |
---|---|---|---|
array_search() + unset | Remove first occurrence | Yes | Optional |
array_keys() + unset | Remove all occurrences | Yes | Optional |
array_diff() | Create new array without value(s) | Yes | Optional |
array_filter() | Create new array with custom logic | Yes | Optional |
Examples Summary
- Remove First
'b'
:
$array = ['a', 'b', 'c', 'b'];
unset($array[array_search('b', $array)]);
// Result: ['a', 'c', 'b']
- Remove All
'b'
s:
$array = ['a', 'b', 'c', 'b'];
foreach (array_keys($array, 'b') as $key) {
unset($array[$key]);
}
// Result: ['a', 'c']
- New Array Without
'b'
:
$array = ['a', 'b', 'c', 'b'];
$filteredArray = array_diff($array, ['b']);
// Result: ['a', 'c']
Final Notes
- Reindexing: Use
array_values()
to reset numeric keys if needed. - Strict Comparison: Use
array_search($value, $array, true)
for exact type checks. - Performance:
array_diff()
andarray_filter()
create new arrays, whileunset()
modifies the original array.