etrader Posted January 28, 2011 Share Posted January 28, 2011 I have a foreach parsing as foreach ($posttags as $tag) { $test = $tag->name; $pattern = "/something/"; if(preg_match($pattern, $test)) { $test2 = "$test is $test"; echo $test2; } } I selected those containing a given word, but I do not know how to remove duplicates to have uniqueness. I was unable to use array_unique, as I am within a foreach loop. Quote Link to comment https://forums.phpfreaks.com/topic/225931-unique-within-foreach/ Share on other sites More sharing options...
QuickOldCar Posted January 28, 2011 Share Posted January 28, 2011 To see the duplicates in the array, must look over the entire new array, so I just do something like this and loop it again. I know of no easier way. foreach ($posttags as $tag) { $test = $tag->name; $pattern = "/something/"; if(preg_match($pattern, $test)) { $test2[] = $test; } } $new_array = array_unique($test2); foreach ($new_array as $final_tags){ echo $final_tags; } Quote Link to comment https://forums.phpfreaks.com/topic/225931-unique-within-foreach/#findComment-1166427 Share on other sites More sharing options...
QuickOldCar Posted January 28, 2011 Share Posted January 28, 2011 I'm guessing can't unique the array first then look for matches? $posttags = array_unique($posttags); Quote Link to comment https://forums.phpfreaks.com/topic/225931-unique-within-foreach/#findComment-1166428 Share on other sites More sharing options...
etrader Posted January 28, 2011 Author Share Posted January 28, 2011 Thanks it works perfectly! Quote Link to comment https://forums.phpfreaks.com/topic/225931-unique-within-foreach/#findComment-1166442 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.