Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.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_Arrays_Algorithm_Graph_Key - Fatal编程技术网

如何在PHP中基于键构建组合数组

如何在PHP中基于键构建组合数组,php,arrays,algorithm,graph,key,Php,Arrays,Algorithm,Graph,Key,在开始解释我的问题之前,我知道很可能已经有大量的示例脚本可以满足我的需要,但老实说,在写作中我无法准确地理解我想要的 我需要计算一个向量,列出所有可能的矩阵组合,其中单个值可能是可能性的关键 为了更好地解释你的意思,请考虑这个例子: $start = 10; $matrix = array( 10 => array(11,12,13), 11 => array(21,31,41), 13 => array(99,98,97), 41 =&

在开始解释我的问题之前,我知道很可能已经有大量的示例脚本可以满足我的需要,但老实说,在写作中我无法准确地理解我想要的

我需要计算一个向量,列出所有可能的矩阵组合,其中单个值可能是可能性的关键

为了更好地解释你的意思,请考虑这个例子:

$start = 10;
$matrix = array(
     10 => array(11,12,13),
     11 => array(21,31,41),
     13 => array(99,98,97),
     41 => array(7,8,9)
)
我需要的是开发一种算法,能够返回所有可能组合的矩阵,考虑到只有当键存在时才能对值进行置换。或者更好(再举一个例子),我希望获得如下输出:

$output = array(
      [0] => array(10 , 11 , 21),   //-- 21 is not a key so the 1st element contains only 3 values
      [1] => array(10 , 11 , 31)
      [2] => array(10 , 11 , 41 , 7)
      [4] => array(10 , 11 , 41 , 8)
      [5] => array(10 , 11 , 41 , 9)
      [6] => array(10 , 12)
      [7] => array(10 , 13 , 99)
      [8] => array(10 , 13 , 98)
      [9] => array(10 , 13 , 97)
 );

有人遇到过类似的问题吗?

通过一些尝试,我可能找到了一个可能的解决方案,如果可以的话:

// variables, start and matrix are the same
$end = array();
$i = 0;
$from ="";
$res = compute($matrix, $start, $from, $i, $end);

// recursive function 
function compute( $matrix, $val, &$from, &$i, &$end){
    // temp base path
    $tmp = $from;
    if( isset($matrix[$val]) ){
        $out = array();
        while(list($c,$item)=each($matrix[$val])){
            if( $c == 0)
                $from .= ($from=="")?($val):(",".$val);

            $r  = compute( $matrix, $item, $from, $i, $end);

            $out[$val][]  = $r;

            if(  is_array($r) ){
                // reset of "base path" to temporary + current val
                $from = ($tmp!="")?($tmp.",".$val):$val;
            }
        }

        return $out;

    }else{
        // ADD ending value to "path"
        $from .=",".$val;
        // ADD complete "path" to END array
        $end[$i] = $from;
        // reset "path" to NODE before
        $from = $tmp;
        // new key for END array
        $i++;

        return $val;
    }
}
然后: 我添加代码填充$end,这对我来说是最困难的,我不确定,在主“核心”函数之后: 印刷品(完);;退回这个:

Array
(
    [0] => 10,11,21
    [1] => 10,11,31
    [2] => 10,11,41,7
    [3] => 10,11,41,8
    [4] => 10,11,41,9
    [5] => 10,12
    [6] => 10,13,99
    [7] => 10,13,98
    [8] => 10,13,97
)
构建$res的代码是递归核心函数, 印刷品(港币);;退回这个:

Array
(
    [10] => Array
        (
            [0] => Array
                (
                    [11] => Array
                        (
                            [0] => 21
                            [1] => 31
                            [2] => Array
                                (
                                    [41] => Array
                                        (
                                            [0] => 7
                                            [1] => 8
                                            [2] => 9
                                        )

                                )

                        )

                )

            [1] => 12
            [2] => Array
                (
                    [13] => Array
                        (
                            [0] => 99
                            [1] => 98
                            [2] => 97
                        )

                )

        )

)

这个问题通常被描述为在有向图中枚举来自给定源的所有路径。这个问题涉及: