1

I have this array here that contains a group of duplicate ids each id is set as as array key and the sub array contains a list of ids that also has another array within the same array?

DUPLICATES 16

array(16) {
  [19503804]=>
  array(3) {
    [0]=>
    string(8) "19501594"
    [1]=>
    string(8) "15539642"
    [2]=>
    string(8) "19498944"
  }
  [19501594]=>
  array(3) {
    [0]=>
    string(8) "19503804"
    [1]=>
    string(8) "15539642"
    [2]=>
    string(8) "19498944"
  }
  [19837033]=>
  array(6) {
    [0]=>
    string(8) "19854557"
    [1]=>
    string(8) "19854558"
    [2]=>
    string(8) "19854553"
    [3]=>
    string(8) "19854565"
    [4]=>
    string(8) "19854554"
    [5]=>
    string(8) "19854683"
  }
  [19854553]=>
  array(6) {
    [0]=>
    string(8) "19854557"
    [1]=>
    string(8) "19854558"
    [2]=>
    string(8) "19837033"
    [3]=>
    string(8) "19854565"
    [4]=>
    string(8) "19854554"
    [5]=>
    string(8) "19854683"
  }
  [19544216]=>
  array(3) {
    [0]=>
    string(8) "19524884"
    [1]=>
    string(8) "19560234"
    [2]=>
    string(8) "19540264"
  }
  [19854565]=>
  array(6) {
    [0]=>
    string(8) "19854557"
    [1]=>
    string(8) "19854558"
    [2]=>
    string(8) "19837033"
    [3]=>
    string(8) "19854553"
    [4]=>
    string(8) "19854554"
    [5]=>
    string(8) "19854683"
  }
  [19854554]=>
  array(6) {
    [0]=>
    string(8) "19854557"
    [1]=>
    string(8) "19854558"
    [2]=>
    string(8) "19837033"
    [3]=>
    string(8) "19854553"
    [4]=>
    string(8) "19854565"
    [5]=>
    string(8) "19854683"
  }
  [15539642]=>
  array(3) {
    [0]=>
    string(8) "19503804"
    [1]=>
    string(8) "19501594"
    [2]=>
    string(8) "19498944"
  }
  [19844271]=>
  array(1) {
    [0]=>
    string(8) "19341140"
  }
  [19498944]=>
  array(3) {
    [0]=>
    string(8) "19503804"
    [1]=>
    string(8) "19501594"
    [2]=>
    string(8) "15539642"
  }
  [16399898]=>
  array(1) {
    [0]=>
    string(8) "15436391"
  }
  [15436391]=>
  array(1) {
    [0]=>
    string(8) "16399898"
  }
  [19341140]=>
  array(1) {
    [0]=>
    string(8) "19844271"
  }
  [19560234]=>
  array(3) {
    [0]=>
    string(8) "19544216"
    [1]=>
    string(8) "19524884"
    [2]=>
    string(8) "19540264"
  }
  [19854683]=>
  array(6) {
    [0]=>
    string(8) "19854557"
    [1]=>
    string(8) "19854558"
    [2]=>
    string(8) "19837033"
    [3]=>
    string(8) "19854553"
    [4]=>
    string(8) "19854565"
    [5]=>
    string(8) "19854554"
  }
  [19540264]=>
  array(3) {
    [0]=>
    string(8) "19544216"
    [1]=>
    string(8) "19524884"
    [2]=>
    string(8) "19560234"
  }
}

notice first array?

    [19503804]=>
  array(3) {
    [0]=>
    string(8) "19501594"
    [1]=>
    string(8) "15539642"
    [2]=>
    string(8) "19498944"
  }

the "19501594" "15539642" "19498944" also has another array which need to be unset.so at the end in this array only the unique arrays with unique values are given.

and this is the expected output required.

array(5) {
  [19503804]=>
  array(3) {
    [0]=>
    string(8) "19501594"
    [1]=>
    string(8) "15539642"
    [2]=>
    string(8) "19498944"
  }
  [19837033]=>
  array(6) {
    [0]=>
    string(8) "19854557"
    [1]=>
    string(8) "19854558"
    [2]=>
    string(8) "19854553"
    [3]=>
    string(8) "19854565"
    [4]=>
    string(8) "19854554"
    [5]=>
    string(8) "19854683"
  }
  [19544216]=>
  array(3) {
    [0]=>
    string(8) "19524884"
    [1]=>
    string(8) "19560234"
    [2]=>
    string(8) "19540264"
  }

  [19844271]=>
  array(1) {
    [0]=>
    string(8) "19341140"
  }
  [16399898]=>
  array(1) {
    [0]=>
    string(8) "15436391"
  }
}

This is what i had initially coded as proposed by @Dale in the answer below.

$theduplicates = array (
  19503804 => 
  array (
    0 => '19501594',
    1 => '15539642',
    2 => '19498944',
  ),
  19501594 => 
  array (
    0 => '19503804',
    1 => '15539642',
    2 => '19498944',
  ),
  19837033 => 
  array (
    0 => '19854557',
    1 => '19854558',
    2 => '19854553',
    3 => '19854565',
    4 => '19854554',
    5 => '19854683',
  ),
  19854553 => 
  array (
    0 => '19854557',
    1 => '19854558',
    2 => '19837033',
    3 => '19854565',
    4 => '19854554',
    5 => '19854683',
  ),
  19544216 => 
  array (
    0 => '19524884',
    1 => '19560234',
    2 => '19540264',
  ),
  19854565 => 
  array (
    0 => '19854557',
    1 => '19854558',
    2 => '19837033',
    3 => '19854553',
    4 => '19854554',
    5 => '19854683',
  ),
  19854554 => 
  array (
    0 => '19854557',
    1 => '19854558',
    2 => '19837033',
    3 => '19854553',
    4 => '19854565',
    5 => '19854683',
  ),
  15539642 => 
  array (
    0 => '19503804',
    1 => '19501594',
    2 => '19498944',
  ),
  19844271 => 
  array (
    0 => '19341140',
  ),
  19498944 => 
  array (
    0 => '19503804',
    1 => '19501594',
    2 => '15539642',
  ),
  16399898 => 
  array (
    0 => '15436391',
  ),
  15436391 => 
  array (
    0 => '16399898',
  ),
  19341140 => 
  array (
    0 => '19844271',
  ),
  19560234 => 
  array (
    0 => '19544216',
    1 => '19524884',
    2 => '19540264',
  ),
  19854683 => 
  array (
    0 => '19854557',
    1 => '19854558',
    2 => '19837033',
    3 => '19854553',
    4 => '19854565',
    5 => '19854554',
  ),
  19540264 => 
  array (
    0 => '19544216',
    1 => '19524884',
    2 => '19560234',
  ),
)

foreach($theduplicates as $k => $v){
    foreach($v as $d){
        if(isset($theduplicates[$d])){
        unset($theduplicates[$d]);
    }
}
// but it totally cleans theduplicates array
array(0) {
}
10
  • 2
    Probably simple enough, but i cant quite work out what you want to do - could you amend your question to include expected output, plus your existing code you have tried to solve the problem Commented May 20, 2016 at 13:40
  • essentially, the values in the first top level array are also present as top level arrays and need removing (i think) Commented May 20, 2016 at 13:45
  • 1
    I dont understand what you are saying or need. I dont see a duplicate id of 19501594 nor that it has another array. Commented May 20, 2016 at 13:46
  • @Steve I have updated the answer the expected output from the above original array needs to be downsized to 5 arrays. Commented May 20, 2016 at 13:48
  • 1
    @CodeGodie, Nope mate you are not slow this morning this scenario is crazy. if you look at the array keys and array values you will see that (starting from top) the array values are each ids and most of them has another sub array in the parent array (as their key). i want to remove those. Commented May 20, 2016 at 13:57

2 Answers 2

2

For every iteration you want to check they keys that have already been iterated. Therefore you want to keep track of these keys. I would do something like this:

$keys = [];
foreach ($main_array as $k => $set) {
    if (in_array($k, $keys)) {
        unset($main_array[$k]);
        continue;
    }
    foreach ($set as $val) {
        $keys[] = $val;
    }
}

var_dump($main_array);

This code will also work, but you have to pass by reference:

foreach ($main_array as &$values) {
    foreach ($values as $value) {
        if(isset($main_array[$value])) {
            unset($main_array[$value]);
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Interesting, I though i am crazy and this is not possible. You just did it.Thank you very much
No problem. Glad I was able to help.
0

If I understand your question (of which I am doubtful) this code should get you on your way

$array = [
    0 => [5, 6, 7],
    1 => [10],
    5 => [10], // to remove
    6 => [10], // to remove
    7 => [10]  // to remove
];

print_r($array);

foreach ($array as $key => $values) {
    foreach ($values as $value) {
        if(isset($array[$value])) {
            unset($array[$value]);
        }
    }
}

print_r($array);

output..

Array
(
    [0] => Array
        (
            [0] => 5
            [1] => 6
            [2] => 7
        )

    [1] => Array
        (
            [0] => 10
        )

    [5] => Array
        (
            [0] => 10
        )

    [6] => Array
        (
            [0] => 10
        )

    [7] => Array
        (
            [0] => 10
        )

)
Array
(
    [0] => Array
        (
            [0] => 5
            [1] => 6
            [2] => 7
        )

    [1] => Array
        (
            [0] => 10
        )

)

2 Comments

@codedudey how are you initially creating the duplicates array in your question?
i know its messy, it comes from a pre-developed script the duplicates are stored in database and is created from mysql results for filtering.

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.