PHP如何通过键从多维父数组中的子数组中取消设置子数组?

时间:2016-05-20 13:35:22

标签: php arrays

这里有一个包含一组重复ID的数组,每个id都被设置为数组键,子数组包含一个id列表,这些ID在同一个数组中也有另一个数组?

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"
  }
}

注意第一个数组?

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

“19501594”“15539642”“19498944”还有另一个需要取消设置的数组。所以在这个数组的最后只给出了具有唯一值的唯一数组。

,这是所需的预期输出。

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"
  }
}

这是我最初编码为@Dale在下面的答案中提出的。

$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) {
}

2 个答案:

答案 0 :(得分:2)

对于每次迭代,您要检查已经迭代过的键。因此,您希望跟踪这些键。我会做这样的事情:

$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);

此代码也可以使用,但您必须通过引用传递

foreach ($main_array as &$values) {
    foreach ($values as $value) {
        if(isset($main_array[$value])) {
            unset($main_array[$value]);
        }
    }
}

答案 1 :(得分:0)

如果我理解你的问题(我怀疑的话),这段代码可以帮助你解决问题

$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);

输出..

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
        )

)