MANWWEBSITE
MANWWEBSITE

Reputation: 33

How do I remove a key from an array if any of it's values contain any string in a separate array?

I have an array, $search_results. Each key has multiple elements. I also have a single column array called $outofstock. If any of the elements within a particular key of $search_results exactly match any of the entries in $outofstock, I would like to remove the key, and re-arrange the key structure, so as to not have a gap in key ordering - and then create a new array of the same name: $search_results.

I've tried a few solutions found here - namely

But I can't seem to get them to match the text exactly and then re-create the array, while using array_filter to remove the entries I need to remove.

Upvotes: 1

Views: 141

Answers (1)

justnajm
justnajm

Reputation: 4534

You can loop through one array and remove the index where you find string you are searching for.

for($i=0;i$<count($outofstock);$i++){   
    foreach($search_result as $k=>$v){   
        if($outofstock[$i]==$v){    
            unset($search_result[$k]);
        }    
    }    
}

$search_result = array_values($search_result);

Upvotes: 1

Related Questions