0

Let me explain my situation, i got myself a multidimensional array.. Down below is the print_r of my array.

    Array
(
    [0] => Array
        (
            [firstname] => Tinga
            [lastname] => 
            [email] => private@information.nl
            [country_code] => NL
            [group] => B2B
            [order_count] => 321
        )

    [1] => Array
        (
            [firstname] => Tinga
            [lastname] => 
            [email] => private@information.nl
            [country_code] => NL
            [group] => B2B
            [order_count] => 12
        )

    [2] => Array
        (
            [firstname] => Rijsbergen Automotive B.V.
            [lastname] => 
            [email] => private@information1.nl
            [country_code] => NL
            [group] => B2B
            [order_count] => 311
        )

    [3] => Array
        (
            [firstname] => Mike Verhoef
            [lastname] => Artis Garage Amsterdam
            [email] => private@information2.nl
            [country_code] => NL
            [group] => B2B
            [order_count] => 260
        )

    [4] => Array
        (
            [firstname] => Marc Kraak
            [lastname] => Vakgarage TEMA
            [email] => private@information3.nl
            [country_code] => NL
            [group] => B2B
            [order_count] => 257
        )

    [5] => Array
        (
            [firstname] => J&B Auto's
            [lastname] => 
            [email] => private@information4.nl
            [country_code] => NL
            [group] => B2B
            [order_count] => 249
        )
)

As you can see, there is a duplicate array, only the order_count is different.

I can easily remove the duplicates using array_unique, but then it removes one of the arrays randomly(i believe).

What i want is to remove the duplicates based on email (private_information) with the least amount of order_count. (So only keep the one with the highest order_count)

Anyone who can help me out here?

u_mulder
  • 54,101
  • 5
  • 48
  • 64
Izzy
  • 61
  • 5
  • 2
    Show sql query which gives you such results. – u_mulder Dec 12 '19 at 14:43
  • What have you tried so far? – Nico Haase Dec 12 '19 at 14:43
  • @u_mulder The results come from a json file. The duplicates are from customers who create more than 1 account on the webshop. – Izzy Dec 12 '19 at 14:47
  • 1
    If you have access to creating this json file - fix it. – u_mulder Dec 12 '19 at 14:49
  • @u_mulder so far i tried building a function myself, was hoping maybe there was already a function i was not aware of that solves my problem. Figured it would save me some time. – Izzy Dec 12 '19 at 14:50
  • @lzzy this might work for you https://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php –  Dec 12 '19 at 14:52
  • @u_mulder i don't have acces to create the file. I need to use the json file and take the data from there. – Izzy Dec 12 '19 at 14:52
  • @skndrkhtr5 it definitely removes the duplicates, but it can remove the wrong array. Im trying to remove the array with the highest order_count. This way the most active account wont get removed. – Izzy Dec 12 '19 at 14:55
  • @lzzy you want to show arrays (max order counts + remove duplicates)? –  Dec 12 '19 at 15:15
  • @skndrkhtr5 yes sir! – Izzy Dec 12 '19 at 15:30

1 Answers1

2

Solution based on provided array is:

$filtered = [];
foreach ($array as $item) {
    $email = $item['email'];

    if (empty($filtered[$email]) || $filtered[$email]['order_count'] < $item['order_count']) {
        $filtered[$email] = $item;
    }
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64