首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数组组合到第n级

将数组组合到第n级
EN

Stack Overflow用户
提问于 2018-10-13 05:03:05
回答 3查看 59关注 0票数 0

我有两个数组。我想从第一个数组中取出一个单词,然后循环通过第二个数组来创建唯一的组合。

代码语言:javascript
复制
$array1 = ['water bottle', 'shed'];
$array2 = ['Rust-proof', 'Double-walled'];

我试图创建一个输出,如:

  • 防锈水瓶
  • 防锈双层水瓶
  • 双壁防锈水瓶
  • 双壁水瓶
  • 防锈棚
  • 防锈双壁棚
  • 双壁防锈棚
  • 双壁棚
EN

回答 3

Stack Overflow用户

发布于 2018-10-13 05:43:23

您可以通过下面的循环很容易地找到所有的组合-

代码语言:javascript
复制
$array1 = ['water bottle', 'shed'];
$array2 = ['Rust-proof', 'Double-walled'];
foreach($array2 as $v1 ) {
foreach($array1 as $v2) {
   echo $v1.' '.$v2."\n";
 }
}

foreach($array2 as $v1 ) {
  foreach($array2 as $v2) {
    if ($v1 != $v2) {
      foreach($array1 as $v3) {
       echo $v1.' '.$v2.' ' .$v3."\n";
      }
    }
  }
}
票数 0
EN

Stack Overflow用户

发布于 2018-10-13 05:45:10

您可以使用我用于循环的任何循环方法来执行类似的操作。

代码语言:javascript
复制
<?php
$array1 = ['water bottle', 'shed'];
$array2 = ['Rust-proof', 'Double-walled'];
$mergedArr = array_merge($array1,$array2);

for($i=0;$i<count($mergedArr);$i++){
    $str = "";
    for($j=$i+1;$j<count($mergedArr);$j++){
        echo $mergedArr[$i].", ".$mergedArr[$j]."\n";
        $str .= ", ".$mergedArr[$j];
    }
    if($i < count($mergedArr)-2)
        echo $mergedArr[$i].$str."\n";
}

现场演示

产出如下:

代码语言:javascript
复制
water bottle, shed
water bottle, Rust-proof
water bottle, Double-walled
water bottle, shed, Rust-proof, Double-walled
shed, Rust-proof
shed, Double-walled
shed, Rust-proof, Double-walled
Rust-proof, Double-walled
票数 0
EN

Stack Overflow用户

发布于 2018-10-13 07:05:34

此函数将为您提供任意大小数组中所有可能的元素组合:

代码语言:javascript
复制
function get_combinations($array) {
    $count = count($array);
    if ($count <= 1) return $array;    
    for ($i = 0; $i < $count; $i++) {
        $elem = $array[$i];
        // add the single element
        $combos[] = array($elem);
        // now join it to the combinations from the remaining array
        if ($i == 0)
            $new_array = array_slice($array, 1);
        elseif ($i == $count)
            $new_array = array_slice($array, 0, $count-1);
        else
            $new_array = array_merge(array_slice($array, 0, $i), array_slice($array,$i+1));
        foreach (get_combinations($new_array) as $arr)
            $combos[] = array_merge(array($elem), is_array($arr) ? $arr : array($arr));
    }
    return $combos;
}

例如

代码语言:javascript
复制
print_r(get_combinations(['Rust-proof', 'Double-walled']));

给出

代码语言:javascript
复制
Array
(
    [0] => Array
        (
            [0] => Rust-proof
        )    
    [1] => Array
        (
            [0] => Rust-proof
            [1] => Double-walled
        )    
    [2] => Array
        (
            [0] => Double-walled
        )    
    [3] => Array
        (
            [0] => Double-walled
            [1] => Rust-proof
        )    
)

得到这个结果后,很容易将其与第一个数组中的每个值组合在一起:

代码语言:javascript
复制
$array1 = ['water bottle', 'shed'];
$array2 = ['Rust-proof', 'Double-walled'];   
foreach ($array1 as $w) {
    foreach (get_combinations($array2) as $arr) {
        echo implode(' ', $arr) . " $w\n";
    }
}

输出:

代码语言:javascript
复制
Rust-proof water bottle
Rust-proof Double-walled water bottle
Double-walled water bottle
Double-walled Rust-proof water bottle
Rust-proof shed
Rust-proof Double-walled shed
Double-walled shed
Double-walled Rust-proof shed

由于该函数是递归的,因此它可以处理任意大小的数组。

代码语言:javascript
复制
$array2 = ['Rust-proof', 'Double-walled', 'Super-duper'];
foreach ($array1 as $w) {
    foreach (get_combinations($array2) as $arr) {
        echo implode(' ', $arr) . " $w\n";
    }
}

输出:

代码语言:javascript
复制
Rust-proof water bottle
Rust-proof Double-walled water bottle
Rust-proof Double-walled Super-duper water bottle
Rust-proof Super-duper water bottle
Rust-proof Super-duper Double-walled water bottle
...
Super-duper shed
Super-duper Rust-proof shed
Super-duper Rust-proof Double-walled shed
Super-duper Double-walled shed
Super-duper Double-walled Rust-proof shed
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52789701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档