将数组组合到第n次

时间:2018-10-13 05:03:05

标签: php

我有两个数组。我想从第一个数组中取出一个单词,然后遍历第二个数组以创建唯一的组合。

$array1 = ['water bottle', 'shed'];
$array2 = ['Rust-proof', 'Double-walled'];

我正在尝试创建如下输出:

  • 防尘水瓶
  • 防锈双层水瓶
  • 双层防锈水瓶
  • 双壁水壶
  • 防锈棚
  • 防尘双层棚
  • 双层防锈棚
  • 双层棚

3 个答案:

答案 0 :(得分:0)

您可以通过以下循环轻松找到所有组合-

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

答案 1 :(得分:0)

您可以使用我用于循环的任何循环方法来做类似的事情

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

Live Demo

输出为:

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

答案 2 :(得分:0)

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

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

例如

print_r(get_combinations(['Rust-proof', 'Double-walled']));

给予

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

已经得到了结果,将其与第一个数组中的每个值组合起来很容易:

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

输出:

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

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

$array2 = ['Rust-proof', 'Double-walled', 'Super-duper'];
foreach ($array1 as $w) {
    foreach (get_combinations($array2) as $arr) {
        echo implode(' ', $arr) . " $w\n";
    }
}

输出:

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