1

I have a multidimensional array and I would like to remove the duplicates from the index if count has same value and keep the array sorting as per provided below. Otherwise, keep the duplicates.

Here's the sample array:-

$array = (
    [0] => array(
        'count' => 3,
        'title' => 'Test title 1',
        'cost' => 300
    ),
    [1] => array(
        'count' => 3,
        'title' => 'Test title 2',
        'cost' => 200
    ),
    [2] => array(
        'count' => 2,
        'title' => 'Test title 3',
        'cost' => 600
    ),
    [3] => array(
        'count' => 2,
        'title' => 'Test title 4',
        'cost' => 500
    ),
);

From the sample array above, we should look for each index if same count value exists. If so, look for next the index and check as well if it has same value, then build a new array like the sample array below.

$newArray = (
    [0] => array(
        'count' => 3,
        'title' => 'Test title 2',
        'cost' => 200
    ),
    [1] => array(
        'count' => 2,
        'title' => 'Test title 4',
        'cost' => 500
    )
);

From the array sample above, we should check on each index if count value has no duplicates. If so, keep on building new array with the same count value.

$newArray = (
    [0] => array(
        'count' => 3,
        'title' => 'Test title 1',
        'cost' => 300
    ),
    [1] => array(
        'count' => 3,
        'title' => 'Test title 2',
        'cost' => 200
    )
);

Here's the code what I've done so far and I'm not sure with what I'm doing here:-

$newArray = [];

foreach ($array as $value) {
    if ($value['count'] == $value['count']) {
        $newArray[] = $value;
    }
    else {
        $newArray[] = $value;
    }
}
3
  • 3
    the seond example isn't clear. What do you mean with "if ALL count values are the same"? There are never ALL count values the same (we have 2 & 3). Commented Apr 10, 2019 at 1:07
  • from the sample array above, we have 2 duplicates count which are count 2 and 3... if count 3 exist then remove the rest of count 3 from the index, same goes with the other index Commented Apr 10, 2019 at 1:10
  • Your array is not really multidimensional. It is rather an array of tuples. You didn't mention it but your array fist has to be sorted by count, otherwise it will be harder or less efficient to remove duplicates. Then you have to iter over the current and next element. For loop with a counter is preferable like for ($i=0; $i<lenght($newArray)-1; ++$i){if($newArray[$i]['count']==$newArray[$i+1]['count']) do something}. After that to have to die by price. Alternatively, you can keep another array with the count and the last index of occureance and use that one for removal. Commented Apr 10, 2019 at 1:13

1 Answer 1

1

This should do it:

$array = [
    ['count' => 3, 'title' => 'Test title 1', 'cost' => 300],
    ['count' => 3, 'title' => 'Test title 2', 'cost' => 200],
    ['count' => 2, 'title' => 'Test title 3', 'cost' => 600],
    ['count' => 2, 'title' => 'Test title 4', 'cost' => 500]
];

// If you want to use the first occurance
$filtered = array_reduce($array, function($acc, $item) {
    if(!in_array($item['count'], array_column($acc, 'count'))) {
        $acc[] = $item;
    }
    return $acc;
}, []);
print_r($filtered);

// Or, if you want to use the use the last occurance
$filtered = array_reduce($array, function($acc, $item) {
    $key = array_search($item['count'], array_column($acc, 'count'));
    if($key!==false) {
        $acc[$key] = $item;
    } else {
        $acc[] = $item;
    }
    return $acc;
}, []);
print_r($filtered);
Sign up to request clarification or add additional context in comments.

3 Comments

This is exactly what I'm looking for. Thank you so much @MiroslavGlamuzina
Out of curiosity, was it the first or second filter you were looking for?
I've tested the first one, but the index order has been changing, so I had to use the second one and just added a few conditions. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.