Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多维数组上的递归函数_Php_Recursion - Fatal编程技术网

Php 多维数组上的递归函数

Php 多维数组上的递归函数,php,recursion,Php,Recursion,我有一个数组,在这个数组中有3个带属性的数组,有时候数组可以有更多或更少的属性 Array ( [0] => Array ( [0] => XS [1] => S [2] => M [3] => L [4] => XL ) [1] => Array ( [0] => Black [1] => Red

我有一个数组,在这个数组中有3个带属性的数组,有时候数组可以有更多或更少的属性

Array
(
[0] => Array
    (
        [0] => XS
        [1] => S
        [2] => M
        [3] => L
        [4] => XL
    )

[1] => Array
    (
        [0] => Black
        [1] => Red
        [2] => Green
    )

[2] => Array
    (
        [0] => Fitted
        [1] => Not Fitted
    )

)
然后我会递归地回显

XS Black Fitted
XS Black Not Fitted
XS Red Fitted
XS Red Not Fitted
XS Green Fitted
XS Green Not Fitted
S Black Fitted
S Black Not Fitted
S Red Fitted
S Red Not Fitted
S Green Fitted
S Green Not Fitted
M Black Fitted
... And so on

我确实有代码,但没有可行的代码来显示任何有意义的东西。递归让我困惑,似乎不能以一种能产生这种效果的方式产生递归函数。。任何帮助都将不胜感激:

这就是从php关联数组实现这种组合所需要的。当我不得不从php关联数组进行这种类型的组合时,我在github gist上发现了这个有用的方法,希望这也能对您有所帮助

<?php
function get_combinations($arrays) {
    $result = array(array());
    foreach ($arrays as $property => $property_values) {
        $tmp = array();
        foreach ($result as $result_item) {
            foreach ($property_values as $property_value) {
                $tmp[] = array_merge($result_item, array($property => $property_value));
            }
        }
        $result = $tmp;
    }
    return $result;
}

$array = [['XS','S','M','L','XL'],['Black','Red','Green'],['Fitted','Not Fitted']];

$combinations = get_combinations($array);
/*
print '<pre>';
print_r($combinations);
print '</pe>';
*/
foreach($combinations as $key=>$value){
   echo implode(' ', $value)."\n";
}
?>

请参阅演示:

这看起来不像是递归的问题,只是嵌套循环应该可以解决它。