Link to home
Start Free TrialLog in
Avatar of morpheus3g
morpheus3g

asked on

delete an array entry

i have an array like this

$array=array("one","two","bingo","three","four");

i want to remove the "bingo" (i know his key)... i tried with " unset($array[2]); but then there's an "hole" in the array...
ASKER CERTIFIED SOLUTION
Avatar of Diablo84
Diablo84

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes if you unset an array element there is a hole in the index, but what's the problem with that. If you use
foreach($array as $key => $value)
 to itterate over the elements instead of a ugly
for ($i=0; $i<count($array); $i++)
It work alwyas, equal if there are holes in the array or not.
You can use sort($array) to get rid of the holes if you don't care about the order of your elements
Avatar of rahulbhanot
rahulbhanot

hi morpheus3g

This is the Best way my dear..........plzzzz  use it and donot worry about the hole......


I assume you mean the missing key.. ie: 0, 1, 3, 4. If you add the array values function the numeric indexes will be reset, eg:

$array=array("one","two","bingo","three","four");
unset($array[2]);
$array = array_values($array);

rahulbhanot