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] => [email protected]
            [country_code] => NL
            [group] => B2B
            [order_count] => 321
        )

    [1] => Array
        (
            [firstname] => Tinga
            [lastname] => 
            [email] => [email protected]
            [country_code] => NL
            [group] => B2B
            [order_count] => 12
        )

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

    [3] => Array
        (
            [firstname] => Mike Verhoef
            [lastname] => Artis Garage Amsterdam
            [email] => [email protected]
            [country_code] => NL
            [group] => B2B
            [order_count] => 260
        )

    [4] => Array
        (
            [firstname] => Marc Kraak
            [lastname] => Vakgarage TEMA
            [email] => [email protected]
            [country_code] => NL
            [group] => B2B
            [order_count] => 257
        )

    [5] => Array
        (
            [firstname] => J&B Auto's
            [lastname] => 
            [email] => [email protected]
            [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?

10
  • 2
    Show sql query which gives you such results. Commented Dec 12, 2019 at 14:43
  • What have you tried so far? Commented Dec 12, 2019 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. Commented Dec 12, 2019 at 14:47
  • 1
    If you have access to creating this json file - fix it. Commented Dec 12, 2019 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. Commented Dec 12, 2019 at 14:50

1 Answer 1

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.