- This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
-
AuthorPosts
-
August 29, 2012 at 7:37 am #10187kirill-kulakovParticipant
Is there an easier way to do so?
$array = array(1,57,5,84,21,8,4,2,8,3,4); $remove = 21; $i = 0; foreach ($array as $value ){ if( $value == $remove) unset($array[$i]) $i++; } //array: 1,57,5,84,8,4,2,8,3,4
August 29, 2012 at 7:39 am #10188mattParticipantThis is a little cleaner:
foreach($array as $key => $value) { if ($value == $remove) { unset($array[$key]); break; } }
UPDATE
Alternatively, you can place the non-matching values into a temp array, then reset the original.
$temp = array(); foreach($array as $key => $value) { if ($value != $remove) { $temp[$key] = $value; } } $array = $temp;
August 29, 2012 at 7:39 am #10189user229044ParticipantIf you want to delete the first occurrence of the item in the array, use
array_search
to find the index of the item in the array rather than rolling your own loop.$array = array(1,57,5,84,21,8,4,2,8,3,4); $remove = 21; $index = array_search($remove, $array); if (index !== false) unset($array[$index]);
To remove all duplicates, rerun the search/delete so long as it finds a match:
while (false !== ($index = array_search($remove, $array))) { unset($array[$index]); }
or find all keys for matching values and remove them:
foreach (array_keys($array, $remove) as $key) { unset($array[$key]); }
August 29, 2012 at 7:42 am #10190mike-brantParticipantThe
array_search
answer is good. You could alsoarraydiff
like this$array = array(1,57,5,84,21,8,4,2,8,3,4); $remove = array(21); $result = array_diff($array, $remove);
-
AuthorPosts
Viewing 4 posts - 1 through 4 (of 4 total)
- You must be logged in to reply to this topic.