delete out of an array.<

I am trying to delete a element out of an array.

say I have an array like so

$new = array("txt","text","jpg","gif","bmp");

now it can be anything. so say we have a form with a text box or 2.

name="delight[]" value="jpg"

so the posted form would be this

$_POST['delight'][0]

but it can be multiple so that is why it is an array itself.
so I have this as the code which will delete one but I belive it breaks to soon so it misses some.

foreach($userkey AS $key=>$index) {
for ($x=0; $x<$total; $x++){
echo "post: ".$_POST['delight'][$x];
if (ereg($_POST['delight'][$x], $index)){
echo "<br />deleted: ".$index."<br />";
$quit++;
break;
} else {
$keep[] $index;
}
}
if ($quit == $total){
break;
}
}

so if I was to delete "jpg" the finished array would be

$keep = array("txt","text","gif","bmp");

understand? any ideas?so you mean, you have a current array from POST, and it deletes all occurences of all of the elements in the POST array from another array?
like if the post array is like array("jpg", "gif") it wold delete "jpg" and "gif" form the other array?foreach($_POST['delight'] as $value) {
if (false !== ($key = array_search($value,$new)) {
unset($new[$key]);
}
}$key = array_search($value,$new)

why did I not think of that. I did use array_search bu tit wasn't doing wha tI wanted to. hmm, let me see what I can do now.

and yes n8.illogique's way worked for me, just add another ) in the if statement. :)
the only thing with that is that it keeps all the old indexes (i dont know if thats a problem)
so you would just have to rework it so it doesnt unset, but adds the ones that dont match to the new array


edit - actually, just make another loop that goes through and adds the left overs to another array to reset it. unless theres another function that resets all the keys.remake the index with
$new = array_values($new);there we go!thanks illogique, that worked. not sure why I was making it harder than it didn't have to be.


thank n8, for seeing that the index will be worng and yes it did matter.
 
Back
Top