View Full Version : Deleting an array element by value

12-01-2006, 11:01 AM
I've been trying to figure out how to delete a part of an array by its value... for example, if I have an array called $numbers with the values 1, 2, 2, and 3 (with arbitrary keys), how would I go about deleting one of the '2's? I tried using the function:

array_diff( $numbers, array(['2'])

The problem is, this deletes ALL of the elements with the value '2' when I only want to delete one of them. Any ideas?

12-01-2006, 11:18 AM
foreach ($numbers as $key => $val)
if ($val == 2)
}and you can reset the keys of your array with this (after you delete your array element):

$numbers2 = array_values($numbers);

12-01-2006, 05:42 PM
Perfect. Thank you!