Skip to content
Advertisement

Remove duplicate subarrays based on identifying data respectively retaining the last occurring duplicates

I get the following data from an api query and I need to remove sets of data with duplicate employee id values and retain the last occurring dataset.

JavaScript

Only 4 of the 10 sets of data belong to unique employee ids, so I need the following output:

JavaScript

All arrays must be sorted by key values [employee][id], If there are no duplicated arrays with the same [employee][id], then just output the solitary array, and if there are for example 4 identical (2,3,5,6), then output the last array (6).

I wrote such cycle, but to me deduces only id of workers, and I need to deduce all last arrays in which these id enter.

JavaScript

Advertisement

Answer

Because php will not allow duplicate keys on any level of an array, you can abuse this rule and assign temporary first-level keys on the result array based on the deep employee id. When finished just re-index the result array.

I am assuming there is no value in retaining the original first level keys.

Your sample input was too verbose, so I have reduced it to its meaningful parts to demonstrate that the last occurring entries are retained.

Code: (Demo)

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement