0

I'm attempting to check the next item in the array's sub-items for matching values of only some of the sub-items, then delete found matches.

I'll eventually need to add code before the deletion which will collect the difference between sub-items if a match is found so I can't simply use array_unique().

The following works to an extent;

 $n = 1;
 for ($i = 0; $i < count($array); $i++) {
    if($array[$i]['author'] == $array[$n]['author']
    && $array[$i]['parentID'] == $array[$n]['parentID']) {
        unset($array[$n]);
        //skip the removed item and continue;
        $i++;
        $n++;
    }
    $n++;
 }

However, it only removes one instance and leaves any matching following conditions. (1 removed below, but 2 not removed)

[0] => Array
    (
        [parentID] => 85560
        [author] => Carla
        [revisions] => 1
    )
[2] => Array
    (
        [parentID] => 85560
        [author] => Carla
        [revisions] => 2
    )
[3] => Array
    (
        [parentID] => 85560
        [author] => Charlie
        [revisions] => 4
    )

Here is the initial input;

[0] => Array
    (
        [parentID] => 85560
        [author] => Carla
        [revisions] => 1
    )
[1] => Array
    (
        [parentID] => 85560
        [author] => Carla
        [revisions] => 2
    )
[2] => Array
    (
        [parentID] => 85560
        [author] => Carla
        [revisions] => 2
    )
[3] => Array
    (
        [parentID] => 85560
        [author] => Charlie
        [revisions] => 4
    )
Aus-tn
  • 31
  • 1
  • 9
  • 1
    Your question is missing the input, without the input noone can make any sense of the output. In the best case, you just provide a testcase on e.g. http://3v4l.org – Xatenev Feb 20 '19 at 15:04
  • 1
    Thanks @Xatenev, updated. – Aus-tn Feb 20 '19 at 15:11
  • This might be a perfect testcase for TDD - what have you tried to spot the error? – Nico Haase Feb 20 '19 at 15:11
  • Favourite from the duplicate (which isn't the accepted answer) is `array_unique($array, SORT_REGULAR)` – Nigel Ren Feb 20 '19 at 15:28
  • @NigelRen you're right, but since the op changed the structure of the array this method cannot be applied anymore. – Mihai Matei Feb 21 '19 at 06:57
  • 1
    @MateiMihai which is why making sure you ask the right question with proper data in the first place is important. I'm also not psychic and don't go through all the questions I've closed checking if they have updated the question to see if it changes my decision. – Nigel Ren Feb 21 '19 at 07:06
  • Hey @NigelRen, yea sorry about that, I updated the post so it wouldn't apply as duplicate anymore. – Aus-tn Feb 21 '19 at 15:37

1 Answers1

2

You can use array_filter for that:

$unique = [];

$array = array_filter($array, function($value) use (&$unique) {
    $key = implode('', $value);

    if(!in_array($key, $unique)) {
        $unique[] = $key;
        return true;
    }

    return false;
});

var_dump($array);

Or, if you don't need to keep the initial indexes, you could use array_reduce:

$array = array_reduce($array, function ($carry, $item) {
    $key = implode('', $item);

    if (!isset($carry[$key])) {
        $carry[$key] = $item;
    }

    return $carry;
}, []);

var_dump(array_values($array));
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50