我有两个数组。我想从第一个数组中取出一个单词,然后循环通过第二个数组来创建唯一的组合。
$array1 = ['water bottle', 'shed'];
$array2 = ['Rust-proof', 'Double-walled'];我试图创建一个输出,如:
发布于 2018-10-13 05:43:23
您可以通过下面的循环很容易地找到所有的组合-
$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";
}
}
}
}发布于 2018-10-13 05:45:10
您可以使用我用于循环的任何循环方法来执行类似的操作。
<?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";
}现场演示
产出如下:
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发布于 2018-10-13 07:05:34
此函数将为您提供任意大小数组中所有可能的元素组合:
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 shedhttps://stackoverflow.com/questions/52789701
复制相似问题