tags:

views:

369

answers:

6

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.

A: 
foreach($display_related_tags as $key => $tag_name)
{
    if($tag_name == $found_tag['name']);
        unset($display_related_tags[$key]);
}
Tatu Ulmanen
You’ve an empty statement as body of your `if`. With that every element will be deleted.
Gumbo
+4  A: 

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]);
    }
}
Gumbo
+1  A: 
foreach($display_related_tags as $key => $tag_name)
{
    if($tag_name == $found_tag['name'])
        unset($display_related_tags[$key];
}
Steve H
'unlink' unlinks files, it has nothing to do with variables or, more specifically, arrays. Perhaps you mean 'unset'?
amn
A: 

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.

Ciarán Walsh
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
+1  A: 

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 );
}
Kevin Peno
Why not just `unset($array['somekey']);`?
Gumbo
This method is useful in cases where you cannot access the array being traversed by the foreach (such as the, bad and overly simplified, second example above). Notice that $array is not available within the function scope.
Kevin Peno
+1  A: 

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.

PiotrN