Removing duplicates from generated multi-select dropdown list

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

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/

Dear Jason,

You are absolutely brilliant! This has removed duplicates from my drop-down box.

Thanks

Terry