Removing duplicates from generated multi-select dropdown list

3 posts by 2 authors in: Forums > CMS Builder
Last Post: September 16, 2013   (RSS)

By terryally - September 16, 2013

Hi guys,

I am populating a drop-down box from a multi-select list (quite similar to the topic covered at http://www.interactivetools.com/forum/forum-topics-create.php?forumNum=791&postNum=2215861).

I am getting duplicate results in that drop-down box and I want to know how to remove them. Example:

Record 1 tags = beans, carros, potatoes

Record 2 tags = tomatoes, carrots, turnips

Record 3 tag = carrots

When I populate the drop-down box I am getting carrots three times:

  • beans
  • carrots
  • carrots
  • carrots
  • potatoes
  • tomatoes
  • turnips

Appreciate your assistance.

Terry

By Jason - September 16, 2013

Hi Terry,

Without seeing your code, its difficult to give a specific example, but it looks like you're going through your records one at a time to add options to your drop down.  Is that right?  If so, I think the best approach would be to gather up all the options into a single array and then use the PHP function array_unique() to get rid of any duplicates.

In this example, it's assuming that you have a field called "list" and that you want to collect up the "values" of that list into your dropdown.

$dropDownValues = array();
  
  foreach ($records as $record) {
    $dropDownValues = array_merge($dropDownValues, $record['list:values']);
  }
  
$dropDownValues = array_unique($dropDownValues);

At the end of this code, $dropDownValues will be an array of all of the list values from your records, with duplicates removed.

Hope this helps get you started

---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/