array_intersect()对n个元素的所有可能组合进行操作。

huangapple go评论76阅读模式
英文:

array_intersect() on all possible combinations of n number of elements

问题

$a1 = [a,b];
$a2 = [a,c];
$a3 = [d,e];
$a4 = [d,f];

我知道使用`array_intersect()`函数可以找到数组之间的共同元素。

所以在这种情况下,我们将执行`array_intersect($a1,$a2,$a3,$a4)`,这将返回一个空数组,因为这4个数组之间没有共同的元素。

如果发生这种情况,我想在所有其他可能的3个数组组合上执行`array_intersect()`

例如

```php
array_intersect($a1,$a2,$a3);  
array_intersect($a1,$a2,$a4);
array_intersect($a1,$a3,$a4);
array_intersect($a2,$a3,$a4);
array_intersect($a2,$a4,$a1);
array_intersect($a2,$a3,$a1);
array_intersect($a2,$a3,$a1);

等等。。

如果其中任何一个未能返回共同元素,这在我们的案例中将会发生,我现在想要在其他可能的2个数组组合上执行array_intersect()

例如

array_intersect($a1,$a2);
array_intersect($a2,$a3);

等等。。

如果我知道我要处理的数组数量,这将起作用,但在我的情况下,数组的数量将是动态的。所以我想要做的是从所有数组开始执行array_intersect(),如果返回空数组,我想在所有其他可能的组合上执行它。

我知道我首先必须开始计算要处理的数组的数量。

$empty_array = [];
array_push($empty_array,$a1,$a2,$a3,$a4);
$arrayCount = count($empty_array);

在这之后,我卡住了,甚至不知道这个方法是否正确。非常感谢任何帮助。


<details>
<summary>英文:</summary>


$a1 = [a,b];
$a2 = [a,c];
$a3 = [d,e];
$a4 = [d,f];


I know using `array_intersect()` function we can find common element between the arrays

So in this case we will do `array_intersect($a1,$a2,$a3,$a4)`, this will return empty array
since there is no common element between the 4 arrays.

If this is happens I want to perform `array_intersect()` on all other possible combinations of 3 arrays.

For example

array_intersect($a1,$a2,$a3);
array_intersect($a1,$a2,$a4);
array_intersect($a1,$a3,$a4);
array_intersect($a2,$a3,$a4);
array_intersect($a2,$a4,$a1);
array_intersect($a2,$a3,$a1);
array_intersect($a2,$a3,$a1);


and so on..

and if any of these fail to return common element, which it will in our case, I would now like to perform
`array_intersect()` on the other possible combinations of 2 arrays.

For example

array_intersect($a1,$a2);
array_intersect($a2,$a3);


and so on..

This would work if I knew the number of arrays that I am working with, but in my case the number of arrays
will be dynamic. So what I want to do is start with `array_intersect()` on all the arrays, and if the empty array is returned,I want to perfom it on all other possible combinations.

I know that I must first start with counting the number of arrays to work with.

$empty_array = [];
array_push($empty_array,$a1,$a2,$a3,$a4);
$arrayCount = count($empty_array);


After this I am stuck,don&#39;t even know if this is even right to begin with. Any help will be greatly appreciated.

</details>


# 答案1
**得分**: 0

```php
$input = [
  [ 'a', 'b' ],
  [ 'a', 'c' ],
  [ 'd', 'e' ],
  [ 'd', 'f' ]
];

$all = array_values(array_unique(array_merge(...$input)));

$result = [];
foreach ($all as $search) {
  $count = 0;
  foreach ($input as $subArray) {
    if (in_array($search, $subArray, true)) {
      $count++;
    }
  }
  $result[$count][] = $search;
}

krsort($result);

foreach ($result as $count => $found) {
  echo "Found $count time(s): " . implode(', ', $found) . "\n";
}

Output:

Found 2 time(s): a, d
Found 1 time(s): b, c, e, f

Another example:

$input = [
  [ 'a', 'b' ],
  [ 'a', 'c' ],
  [ 'd', 'e', 'a' ],
  [ 'd', 'f', 'a' ]
];

...

Found 4 time(s): a
Found 2 time(s): d
Found 1 time(s): b, c, e, f
英文:
$input = [
  [ &#39;a&#39;, &#39;b&#39; ],
  [ &#39;a&#39;, &#39;c&#39; ],
  [ &#39;d&#39;, &#39;e&#39; ],
  [ &#39;d&#39;, &#39;f&#39; ]
];

$all = array_values(array_unique(array_merge(...$input)));

$result = [];
foreach ($all as $search) {
  $count = 0;
  foreach ($input as $subArray) {
    if (in_array($search, $subArray, true)) {
      $count++;
    }
  }
  $result[$count][] = $search;
}

krsort($result);

foreach ($result as $count =&gt; $found) {
  echo &quot;Found $count time(s): &quot; . implode(&#39;, &#39;, $found) . &quot;\n&quot;;
}

Output:

Found 2 time(s): a, d
Found 1 time(s): b, c, e, f

Another example:

$input = [
  [ &#39;a&#39;, &#39;b&#39; ],
  [ &#39;a&#39;, &#39;c&#39; ],
  [ &#39;d&#39;, &#39;e&#39;, &#39;a&#39; ],
  [ &#39;d&#39;, &#39;f&#39;, &#39;a&#39; ]
];

...

Found 4 time(s): a
Found 2 time(s): d
Found 1 time(s): b, c, e, f

huangapple
  • 本文由 发表于 2023年7月12日 23:20:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定