With some attemps, MAYBE i found a possible solution, if can be usefull :
// 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;
}
}
Then: I add code to fill $end, most difficult for me and i am not sure about it, after main "core" function: print_r($end); return this:
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
)
code to build $res is the function recursive core, print_r($res); return this :
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
)
)
)
)