Is there an Array_unique equivalent for Kirby Object to deduplicate pages

Hi everyone,

I need to make a list of software developers from the software description pages.

            fields:
              categories:
                label: Catégories de logiciel
                type: tags
                options:
                  type: query
                  fetch: site.logicielsCat.toStructure
                  text: "{{ item.name }}"
                  value: "{{ item.slug }}"
              editeur:
                label: Editeur
                type: text
                required: true
                width: 3/4
              poids:
                label: Poids
                type: text
                required: true
                width: 1/4
              logiciel:
                label: Logiciel
                type: text
                required: true
                width: 3/4
              description:
                label: Description
                type: writer
              web:
                label: Site Web
                type: url
                width: 3/4
              adresse:
                label: Adresse
                type: text
              cp:
                label: Code-postal
                type: text
                width: 1/4
              localite:
                label: Localité
                type: text
                width: 1/2
              tel:
                label: Tél
                type: tel
                width: 1/4

Obviously, different software may have been developed by the same developer, and I end up with many duplicates when I extract the complete list.
I’d like to be able to deduplicate this software list to obtain a listing of developers keeping the collection as a Kirby object.
So is there a Kirby-specific method that does the same job as array_unique()?

Could you post your code please, not quite sure what you are trying to do and where based on that blueprint excerpt.

Hi Texnixe,

Sorry for my late reply, in the meantime I’ve had to take on another project.
For now I store in an array all needed data from a collection, then I loop through the original array, then I check if the value for the “editeur” key already exists in a temporary associative array. If it doesn’t exist, add it to the result array and mark it as seen in the temporary array.

$softwares = $kirby->collection("logiciels");

$inputArray = [];
foreach ($softwares as $key => $soft) {
  $inputArray[] = [
    'editeur' => $soft->editeur(),
    'cp' => $soft->cp(),
    'ville' => $soft->ville(),
    'logiciel' => $soft->logiciel(),
    'path' => $key,
  ];
}

// Array to hold unique results
$uniqueArray = [];
$seen = [];

// Loop through the input array
foreach ($inputArray as $item) {
  // Get the editeur value using the value() method
  $editeurValue = $item['editeur']->value();

  // Check if this editeur has already been seen
  if (!in_array($editeurValue, $seen)) {
    // Add to unique results
    $uniqueArray[] = $item;
    // Mark this editeur as seen
    $seen[] = $editeurValue;
  }
}

// Output the unique array
print_r($uniqueArray);

So I was wondering if there wasn’t a more direct way (using a kirby method) of deduplicating a collection on a given field?

Thanks

$values = [];
$unique = ($softwares->filter(function($child) use (&$values) {
    if (!in_array($child->editeur()->value(), $values, true)) {
	    $values[] = $child->editeur()->value();
        return $child;
    }
}));

It seems the way I wrote the query for collection is wrong, because I got each page in two versions, the first one is the regular page slug and the second is the path starting with the “content” directory.

# site/theme/collections/logiciels.php
return function ($site, $page) {
    $all = $site->index()->template('logiciel')->published()->sortBy('categorie', 'logiciel');
    return $all;
};

I see in your code you used a sort of callback function in the filter() method, where can I find the documentation of these methods?

In our reference: Reference | Kirby CMS, this one in particular: $pages->filter() | Kirby CMS

Hm, I’m afraid I don’t understand what you are trying to say here.

Sorry to not be clear enough.

A dump($kirby->collection("logiciels")) outputs the same page in two versions with and without the “content” directory:

Kirby\Cms\Pages Object
(
    [0] => a-propos/logiciels-interfaces/logiciels-centres-de-sante/acteur-fr
    [1] => content/a-propos/logiciels-interfaces/logiciels-centres-de-sante/acteur-fr
    [2] => a-propos/logiciels-interfaces/logiciels-centres-de-sante/amies
    [3] => content/a-propos/logiciels-interfaces/logiciels-centres-de-sante/amies

No idea where that’s coming from, that’s definitely not the expected behavior. do you have any models or plugins in place that redefine anything?

BTW: Do you just need a flat list of the developers, then using pluck would be the way to go.

You’re right a model had already a query doing the same.

Thanks,