Jump to content

unique within foreach


etrader

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/225931-unique-within-foreach/
Share on other sites

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.