...

View Full Version : Deleting an array element by value



shanstafari
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?

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


$numbers2 = array_values($numbers);

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum