PHP array delete by value (not key) ?

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

MethodUse CaseKey PreservationReindexing Needed?
array_search() + unsetRemove first occurrenceYesOptional
array_keys() + unsetRemove all occurrencesYesOptional
array_diff()Create new array without value(s)YesOptional
array_filter()Create new array with custom logicYesOptional

Examples Summary

  1. Remove First 'b':
   $array = ['a', 'b', 'c', 'b'];
   unset($array[array_search('b', $array)]);
   // Result: ['a', 'c', 'b']
  1. Remove All 'b's:
   $array = ['a', 'b', 'c', 'b'];
   foreach (array_keys($array, 'b') as $key) {
       unset($array[$key]);
   }
   // Result: ['a', 'c']
  1. 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() and array_filter() create new arrays, while unset() modifies the original array.

Leave a Reply

Your email address will not be published. Required fields are marked *