0

我有以下数据结构:

array (size=3)
  0 => 
    array (size=4)
      0 => string 'apple' (length=5)
      1 => string 'colophon' (length=8)
      2 => string 'byo-fusion-drive' (length=16)
      3 => string 'scroll-targeting' (length=16)
  1 => 
    array (size=3)
      0 => string 'apply' (length=5)
      1 => string 'exploring-web-typography' (length=24)
      2 => string 'on-performance-content-management' (length=33)
  2 => 
    array (size=3)
      0 => string 'macbook' (length=7)
      1 => string 'colophon' (length=8)
      2 => string 'nifty-minidrive' (length=15)

我试图找出我的数组集合中的哪些数组(如果有)共享一个共同的值。

例如:数组 0 和 2 共享字符串“colophon”。

我尝试过使用 array_intersect ,但这当然会返回 NULL,因为数组 1 与其他数组没有共同的值。

此外,在任何给定的集合中都可能(甚至可能)没有共同价值。数组的集合将始终包含至少两个数组。集合中可能有任意数量的附加数组。

有了上面描述的数据,最终结果应该是这样的:

array (size=2)
  0 => 
    array (size=4)
      0 => string 'apple' (length=5)
      1 => string 'colophon' (length=8)
      2 => string 'byo-fusion-drive' (length=16)
      3 => string 'scroll-targeting' (length=16)
  1 => 
    array (size=3)
      0 => string 'macbook' (length=7)
      1 => string 'colophon' (length=8)
      2 => string 'nifty-minidrive' (length=15)

即:删除数组 1(从原始数组),因为它没有共同的价值。

我确信有一种简单的方法可以做到这一点,但我已经尝试了 8 个多小时,并决定寻求帮助。

任何人?

4

2 回答 2

1

尝试

$arr = array (
  array('apple', 'colophon', 'byo-fusion-drive', 'scroll-targeting', ),
  array('apply', 'exploring-web-typography', 'on-performance-content-management', ),
  array('macbook', 'colophon', 'nifty-minidrive', 'nifty-minidrive', ),
);

$repeated_values = array_keys(
  array_filter(
    array_count_values(
      array_reduce($arr, function ($res, $value) {
          return array_merge($res, array_unique($value));
        }, array()
      )
    ), function ($count) {
      return $count > 1;
  })
);

$result = array_filter($arr, function($value) use ($repeated_values) {
   if (sizeof(array_intersect($repeated_values, $value)) > 0) return true;
});

var_dump($result);

输出

array (size=2)
  0 => 
    array (size=4)
      0 => string 'apple' (length=5)
      1 => string 'colophon' (length=8)
      2 => string 'byo-fusion-drive' (length=16)
      3 => string 'scroll-targeting' (length=16)
  2 => 
    array (size=4)
      0 => string 'macbook' (length=7)
      1 => string 'colophon' (length=8)
      2 => string 'nifty-minidrive' (length=15)
      3 => string 'nifty-minidrive' (length=15)
于 2012-11-03T13:46:10.650 回答
1

您需要使用嵌套循环对其进行迭代。像这样:

$array = array(
    array(
        "apple",
        "colophon",
        "byo-fusion-drive",
        "scroll-targeting"
    ),
    array(
        "apply",
        "exploring-web-typography",
        "on-performance-content-management"
    ),
    array(
        "macbook",
        "colophon",
        "nifty-minidrive"
    )
);

for ($i = 0; $i < count($array); $i++) {
    for ($j = $i+1; $j < count($array); $j++) {
        var_dump(array_intersect($array[$i], $array[$j]));
    }
}

哪个输出:

array (size=0)
  empty
array (size=1)
  1 => string 'colophon' (length=8)
array (size=0)
  empty

对循环的简单修改给出了预期的行为:

$result = array();

for ($i = 0; $i < count($array); $i++) { //Start from the first array, and continue up to all of them.
    for ($j = $i+1; $j < count($array); $j++) { //Start with the current array of $i, +1. So that collisions never occur.
        if (count(array_intersect($array[$i], $array[$j])) !== 0) { //If there are common values (the array_intersect() function returns a non-empty array
            if (!in_array($array[$i], $result)) $result[] = $array[$i]; //Add the first array (if it's not there already)
            if (!in_array($array[$j], $result)) $result[] = $array[$j]; //Add the second array (if it's not there already)
        }
    }
}

哪个输出

array (size=2)
  0 => 
    array (size=4)
      0 => string 'apple' (length=5)
      1 => string 'colophon' (length=8)
      2 => string 'byo-fusion-drive' (length=16)
      3 => string 'scroll-targeting' (length=16)
  1 => 
    array (size=3)
      0 => string 'macbook' (length=7)
      1 => string 'colophon' (length=8)
      2 => string 'nifty-minidrive' (length=15)
于 2012-11-03T13:42:05.453 回答