Remove duplicate phrases in a foreach loop

I have the following problem:

In my WordPress 3.6 I have for posts a custom field called: “topics”.
The field is automatically filled with phrases from another website by RSS, and it adds multiple criteria. It is filled in like this: “Cars,Bikes,Stuff,Gadget”

Read More

When I query wordpress with get_posts I get in my foreach loop this:

Cars,Bikes,Stuff,Gadget
Cars,Bikes
Bikes,Stuff
Gadget

I put this into a string and replace some stuff:

$topic_filter = get_posts(
 array(
  'numberposts' => -1,
  'post_status' => 'private',
 )
);
$search_topic = array(' ', '-&-', '-|-', '-/-', '---');
$replace_topic = array("-", "-", "-", "-", "-", "");

foreach ($topic_filter as $post_topic) {
$str = str_replace($search_topic, $replace_topic, get_post_meta($post_topic->ID,     'topic', true));

echo $str;

}

final echo putput is then:

Cars,Bikes,Stuff,Gadget,Cars,Bikes,Bikes,Stuff,Gadget

So far so good. But how to remove the duplicates now?

I tried implode / explode, but it doesn’t do anything, because the items are in a foreach loop I think, and it only apply inside for each post.

But I need the foreach loop, because in the end the goal is to get this cleaned string as a list in an html output something like this:

<input
  type="button"
  value="Cars"
  class="filter-button"

  data-control-type="button-filter"
  data-control-action="filter"
  data-control-name="Cars-btn" 
  data-path=".Cars" 
/>
<input
  type="button"
  value="Bikes"
  class="filter-button"

  data-control-type="button-filter"
  data-control-action="filter"
  data-control-name="Bikes-btn" 
  data-path=".Bikes" 
/>
<input
  type="button"
  value="Gadget"
  class="filter-button"

  data-control-type="button-filter"
  data-control-action="filter"
  data-control-name="Gadget-btn" 
  data-path=".Gadget" 
/>

seems pretty complicated to me 🙁

any ideas? I would be really happy for your help!

Thanks!

Related posts

Leave a Reply

1 comment