Php 从多个嵌套数组中获取所有组合

Php 从多个嵌套数组中获取所有组合,php,arrays,algorithm,logic,Php,Arrays,Algorithm,Logic,我试图用PHP提出一种算法,以获得嵌套数组的所有组合: Array ( [0] => Array ( [0] => Option Object ( [strValue] => rough ) [1] => Option Object (

我试图用PHP提出一种算法,以获得嵌套数组的所有组合:

Array
(
    [0] => Array
        (
            [0] => Option Object
                (
                    [strValue] => rough
                )

            [1] => Option Object
                (
                    [strValue] => smooth
                )

            [2] => Option Object
                (
                    [strValue] => coarse
                )

        )

    [1] => Array
        (
            [0] => Option Object
                (
                    [strValue] => shiney
                )

            [1] => Option Object
                (
                    [strValue] => mat
                )

        )

    [2] => Array
        (
            [0] => Option Object
                (
                    [strValue] => Large
                )

            [1] => Option Object
                (
                    [strValue] => Medium
                )

            [2] => Option Object
                (
                    [strValue] => Small
                )

            [3] => Option Object
                (
                    [strValue] => very large
                )

        )

)
所以我会得到一些回报,比如:

-粗的,亮的,大的

-粗糙的,光滑的,小的

-粗糙的,光滑的,中等的

-粗糙的,雪尼的,很大的

-光滑的,光滑的,大的

-光滑、光亮、小

-光滑、光亮、中等

-光滑,光滑,非常大

etc(本例中应为24)

我尝试了各种foreach示例和一些基本的递归函数,但似乎没有什么进展。如果有人能给出解决这个问题的基本思路,我将非常感激,谢谢

以下是psuedo PHP中的暴力(最差效率)算法:

$array1 = $array[0];
$array2 = $array[1];
$array3 = $array[2];

$results = array();
for( $i = 0; $i < count( $array1); $i++)
    for( $j = 0; $j < count( $array2); $j++)
        for( $k = 0; $k < count( $array3); $k++)
            $results[] = $array1[$i] . ',' . $array2[$j] . ',' . $array3[$k];
$array1=$array[0];
$array2=$array[1];
$array3=$array[2];
$results=array();
对于($i=0;$i
是时候嵌套一些foreach循环了

<?php
$array1 = array('rough', 'smooth', 'coarse');
$array2 = array('shiny', 'matte');
$array3 = array('very large', 'large', 'medium', 'small');

foreach($array1 as $i)
    foreach($array2 as $j)
        foreach($array3 as $k)
            $output[] = "$i, $j, $k";

var_dump($output);
/* ouput
array
  0 => string 'rough, shiny, very large' (length=24)
  1 => string 'rough, shiny, large' (length=19)
  2 => string 'rough, shiny, medium' (length=20)
  3 => string 'rough, shiny, small' (length=19)
  4 => string 'rough, matte, very large' (length=24)
  5 => string 'rough, matte, large' (length=19)
  6 => string 'rough, matte, medium' (length=20)
  7 => string 'rough, matte, small' (length=19)
  8 => string 'smooth, shiny, very large' (length=25)
  9 => string 'smooth, shiny, large' (length=20)
  10 => string 'smooth, shiny, medium' (length=21)
  11 => string 'smooth, shiny, small' (length=20)
  12 => string 'smooth, matte, very large' (length=25)
  13 => string 'smooth, matte, large' (length=20)
  14 => string 'smooth, matte, medium' (length=21)
  15 => string 'smooth, matte, small' (length=20)
  16 => string 'coarse, shiny, very large' (length=25)
  17 => string 'coarse, shiny, large' (length=20)
  18 => string 'coarse, shiny, medium' (length=21)
  19 => string 'coarse, shiny, small' (length=20)
  20 => string 'coarse, matte, very large' (length=25)
  21 => string 'coarse, matte, large' (length=20)
  22 => string 'coarse, matte, medium' (length=21)
  23 => string 'coarse, matte, small' (length=20)
*/
?>

我刚刚写了这个,它适用于任何长度的数组

<?php

function cartesian_product($a) {
  $result = array(array());
  foreach ($a as $list) {
    $_tmp = array();
    foreach ($result as $result_item) {
      foreach ($list as $list_item) {
        $_tmp[] = array_merge($result_item, array($list_item));
      }
    }
    $result = $_tmp;
  }
  return $result;
}


// Let's test this..                                                                                                                                                                                    

header('Content-type: text/plain');

$a = array(
  array('rough','smooth','coarse'),
  array('shiney','mat'),
  array('small','medium','large','x-large'),
);

$result = cartesian_product($a);
foreach ($result as $row) {
  print implode(", ", $row) ."\n";
}

您的代码(理想情况下会添加到问题中)到底是如何失败的?因为乍一看,解决您的问题只需要一组3个嵌套for循环,我不认为数组是嵌套的。它们都包含在父数组中,但它们彼此不嵌套。但要创建一个使用它们的循环,您需要嵌套的循环。井的可能重复不一定只需要3个foreach。上面可以有任意数量的额外元素。如果你有机会尝试一下你提到的笛卡尔函数,那就完美了。谢谢!正如我刚才所评论的,它不一定只需要3个foreach。上面可以有任意数量的额外元素,这是动态的,所以我不能将它限制为3个嵌套for/foreachOh,我的错误。你的标题有误导性,因为它特别提到了3个数组。哦,仅供参考,在Python中,你可以这样做:)感谢你提供的输出,使回复易于验证