Need help removing duplicates from a generated list

I’m trying to create a list of items for a dropdown filter. So after using a series of pdoResources, getImageIists and other snippets to get the finalized output of available data, I have an output that looks something like this: Admission|Second Admission|Admission, 3| and so on (a few hundred results). I specifically divided them by pipes because some results have commas in them.

I then added a snippet to my original pdoResources placeholder like this: [[+listItems:removeDupsList]] to visually check my output for errors.

Here is what I did for my snippet:

<?php
$list = explode('|', $input);

foreach ($list as $x => $value) :
    echo "$value<br>";
endforeach;

This gave me my main list. To remove duplicates, I tried this:

<?php
$list_input = explode('|', $input);
$list = array_unique($list_input);

foreach ($list as $x => $value) :
    echo "$value<br>";
endforeach;

This resulted in some duplicates being removed, but not all and I cannot figure out why. I even tried using my snippet without any html in it like one article suggested:

$list_input = explode('|', $input);
$list = array_unique($list_input);

foreach ($list as $x => $value) :
    $output = $modx->getChunk('list-itemTpl',array(
      'myItem' => $value,
    ));
    return $output;
endforeach;

And the list-itemTpl contains this line, but resulted in only the first item:
<p>[[+myItem]]</p>

Any help would be appreciated. Is there a better way of doing this? Thanks.

It would usually be much better to write a snippet that only returns the data you want. Perhaps someone can help if you give us some more information about how you’re generating the list…

In that last one you’ve got the return statement inside the foreach loop so it returns after the first execution and won’t loop.

Try this:

$output = '';
foreach ($list as $x => $value) :
    $output .= $modx->getChunk('list-itemTpl',[
      'myItem' => $value,
    ]);
endforeach;
return $output;

Disclaimer: I don’t know what the values are, but this should fix your early return problem.

Explanation

  • Declaring the $output variable before the loop means you can access it after the loop has finished.
  • Using the .= operator instead of = means it will append the value instead of replacing it.
  • Returning $output after the loop means that the loop will get to finish before exiting.

Are these duplicates that remain really exactly the same string. array_unique is case sensitive for example or a trailing space can also be a reason why two strings are different.

In this example, no duplicates are found.

$input = "Admission|Admission |admission";
$list_input = explode('|', $input);
$list = array_unique($list_input);

This is off the top of my head, so it may be wrong, but you might try this:

$list_input = array_map("trim", explode('|', $input) );

That should remove leading and trailing spaces from all array members.

Thanks but for some reason that still doesn’t work. I’ve checked everywhere, in the MIGX list, the custom DB that it pulls from and just showing the array values in a print_r command. But these spaces keep showing up for no reason.

All I want to do is output a list of items from the MIGX configs list that are currently applied to each of our staff. Just to produce a drop-down list that is used to filter them. I figured outputting a list minus their names would be a reasonable way to do this dynamically and then filter out the duplicates. But for some weird reason these rogue spaces are interfering with that duplication removal.

So frustrating!

could you post the whole output of the unmodified output of all listItems?

is this what you wanted? Or something else? I’ve expanded it to 10 people.

Washington DC
FloridaFlorida Southern District Court
Texas
Washington DC
PennsylvaniaNew Jersey
Pennsylvania
Pennsylvania
Washington DCWashington DC District CourtPennsylvania
PennsylvaniaPennsylvania Middle District Court
VirginiaMarylandWashington DCVirginia Eastern District CourtMaryland District CourtWashington DC District CourtNorth Dakota District CourtFourth Circuit Court of AppealsEighth Circuit Court of Appeals

is this the output from [[+listItems]]?
I don’t see any pipes there, only new lines and spaces?

where is this placeholder coming from?

I think i pulled that from the output in the source code.

Here’s what I get with just 3 resources:

District of Columbia|| Florida||U.S. District Court for the Southern District of Florida|| Texas||

I think I figured out what the problem is. The extra space is getting inserted at the end of each record. From this example, there is a space before Florida and Texas, Those are the starts of new records in the getImageList result. Is there a way to fix the record delimiter from this? rather than a space just use || instead?

Thanks

Woo Hoo!! I figured it out! I don’t have time to explain right now but i’ll add the fix here later.

thank you all for your help!!