-3

How remove and reindex arrays duplicate by comparing spécific values source and target?

Sample array to process:

Array
(
    [links] => Array
        (
            [0] => Array
                (
                    [source] => galaxy
                    [target] => s7
                    [value] => 1
                )
            [1] => Array
                (
                    [source] => galaxy
                    [target] => s7
                    [value] => 1
                )           
            [2] => Array
                (
                    [source] => s7
                    [target] => galaxy
                    [value] => 1
                )
            [3] => Array
                (
                    [source] => galaxy
                    [target] => s8
                    [value] => 1
                )
        )
)

Desired result:

Array
(
    [links] => Array
        (
            [0] => Array
                (
                    [source] => galaxy
                    [target] => s7
                    [value] => 1
                )
            [1] => Array
                (
                    [source] => s7
                    [target] => galaxy
                    [value] => 1
                )               
            [2] => Array
                (
                    [source] => galaxy
                    [target] => s8
                    [value] => 1
                )
        )
)

Thank's

Sandra
  • 1,596
  • 15
  • 22
  • Possible duplicate of [How to remove duplicate values from a multi-dimensional array in PHP](https://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – ficuscr Jul 20 '18 at 18:25
  • @ficuscr please, take the time to read the question carefully, cordially. – Sandra Jul 20 '18 at 18:31
  • I almost feel like this is rhetorical anyway... https://stackoverflow.com/questions/591094/how-do-you-reindex-an-array-in-php/6507615#6507615 – ficuscr Jul 20 '18 at 18:57
  • @ficuscr it's not the same question because the need is different. Please, stay friendly. cordiality – Sandra Jul 20 '18 at 19:09
  • Always. If it's not a dupe it's not a dupe. I'm just failing to understand you obviously. I'll leave the link and retract the close vote. Nothing personal. Happy Friday. – ficuscr Jul 20 '18 at 19:13
  • 1
    When you say "comparing specific values source and target" does that mean that the value key may have different values for the same source and target? If that's the case, which value should be kept? And if that's not the case, can you explain a bit more how this is different than the suggested duplicate? – Don't Panic Jul 20 '18 at 19:21

1 Answers1

2

Are you saying the linked answer does not work because of the "reindex" aspect? I did read carefully I thought. Since you shared no code maybe elaborate a bit if I am still missing something?

Here:

 <?php
 $foo = [
   ['source' => 'g', 'target' => 's7'],
   ['source' => 'g', 'target' => 's7'],
   ['source' => 's7', 'target' => 'g'],
   ['source' => 'g', 'target' => 's8']
];

var_dump(array_values(array_unique($foo, SORT_REGULAR)));
array(3) {
  [0]=>
  array(2) {
    ["source"]=>
    string(1) "g"
    ["target"]=>
    string(2) "s7"
  }
  [1]=>
  array(2) {
    ["source"]=>
    string(2) "s7"
    ["target"]=>
    string(1) "g"
  }
  [2]=>
  array(2) {
    ["source"]=>
    string(1) "g"
    ["target"]=>
    string(2) "s8"
  }
}
ficuscr
  • 6,975
  • 2
  • 32
  • 52