vote up 0 vote down star
2

i want to loop an array with foreach to check if a value exist. and if it does, i want to delete that element.

i have following code:

    foreach($display_related_tags as $tag_name)
    {
        if($tag_name == $found_tag['name']);
            // DELETE CODE
    }

but i dont know how to delete the element once the value is found. how do i do that?

there are maybe other alternatives/functions to do that and you are all welcome to share them. however, i have to use foreach with this one here. thanks.

flag

6 Answers

vote up 4 vote down check

If you also get the key, you can delete that item like this:

foreach ($display_related_tags as $key => $tag_name) {
    if($tag_name == $found_tag['name']) {
        unset($display_related_tags[$key]);
    }
}
link|flag
vote up 1 vote down
foreach($display_related_tags as $key => $tag_name)
{
    if($tag_name == $found_tag['name']);
        unset($display_related_tags[$key]);
}
link|flag
You’ve an empty statement as body of your if. With that every element will be deleted. – Gumbo Dec 22 at 22:07
vote up 1 vote down
foreach($display_related_tags as $key => $tag_name)
{
    if($tag_name == $found_tag['name'])
        unlink($display_related_tags[$key];
}
link|flag
vote up 1 vote down

This will delete it from within the external array without the need to call unset on the offset (key) on the external array.

foreach( $array as $key => &$value )
{
    if( $key === "somekey")
        unset( $value );
}

This method is useful in cases where you cannot access the array being traversed by the foreach (such as the, bad and overly simplified, example below).

foreach( $array as $key => &$value )
{
    somefunction( $key, $value );
}
function somefunction( $k, &$v )
{
    if( $k === "somekey")
        unset( $v );
}
link|flag
Why not just unset($array['somekey']);? – Gumbo Dec 22 at 22:04
vote up 0 vote down

As has already been mentioned, you’d want to do a foreach with the key, and unset using the key – but note that mutating an array during iteration is in general a bad idea, though I’m not sure on PHP’s rules on this offhand.

link|flag
A php foreach will execute on the entire array regardless. Test unsetting a value that is next in iteration. It will iterate on the offset, but the value will be null. – Kevin Peno Dec 22 at 21:31
vote up 0 vote down

Instead of doing foreach() loop on the array, it would be faster to use array_search() to find the proper key. On small arrays, I would go with foreach for better readibility, but for bigger arrays, or often executed code, this should be a bit more optimal:

$result=array_search($unwantedValue,$array,true);
if($result !== false) {
  unset($array[$result]);   
}

The strict comparsion operator !== is needed, because array_search() can return 0 as the index of the $unwantedValue.

Also, the above example will remove just the first value $unwantedValue, if the $unwantedValue can occur more then once in the $array, You should use array_keys(), to find all of them:

$result=array_keys($array,$unwantedValue,true)
foreach($result as $key) {
  unset($array[$key]);
}

Check http://php.net/manual/en/function.array-search.php for more information.

link|flag

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.