Wednesday, July 22, 2009

How to delete element in the Array in php if it exists

One of friend is asking on this issue,so post it in the blog then it will be useful to all us ...
The following code will be delete the element in an array if that element is exists
in the array.


<?

$numbers = array(1,2,3,4,5);

for($i=0;$i
{
if($numbers[$i]==3)
{
unset($numbers[$i]); // removes "4"
}
}
print_r($numbers);

?>

Hope it will be useful.

No comments:

Post a Comment