-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

6
  • Possible duplicate of How to remove duplicate values from a multi-dimensional array in PHP Commented Jul 20, 2018 at 18:25
  • @ficuscr please, take the time to read the question carefully, cordially. Commented Jul 20, 2018 at 18:31
  • I almost feel like this is rhetorical anyway... stackoverflow.com/questions/591094/… Commented Jul 20, 2018 at 18:57
  • @ficuscr it's not the same question because the need is different. Please, stay friendly. cordiality Commented Jul 20, 2018 at 19:09
  • 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? Commented Jul 20, 2018 at 19:21

1 Answer 1

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

1 Comment

Same here, looks reasonable to me. Too many question marks maybe? ;)

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.