Нужно решить некоторые проблемности в коде и еще подключить некоторые функции.
Есть такой код, который сделал
Андрей Ежгуров вначале его нужно немного доработать.
$a="1 2 3 4 5"; // будем работать с числами, с ними проще понять систему.
$m=generate(preg_split('#[[:space:]]+#s', trim($a)));
print_r($m);
function generate($inp){
$out=array(implode(' ', $inp));
$head=array();
while(count($inp)>1){
$head[]=array_shift($inp);
foreach(generate($inp) as $val){
$out[]=array_merge(array(implode(' ', $head)), (array)$val);
}
}
return $out;
}
Вариант 2 - почти то но не то :)
function generate($inp){
$out=array(implode(' ', $inp));
$tail=array();
while(count($inp)>1){
array_unshift($tail, array_pop($inp));
foreach(generate($tail) as $val){
$out[]=array_merge(array(implode(' ', $inp)), (array)$val);
}
}
return $out;
}
код выдает такие комбинации массива
Array
(
[0] => 1 2 3 4 5
[1] => Array
(
[0] => 1
[1] => 2 3 4 5
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 3 4 5
)
[3] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4 5
)
[4] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
[5] => Array
(
[0] => 1
[1] => 2
[2] => 3 4
[3] => 5
)
[6] => Array
(
[0] => 1
[1] => 2 3
[2] => 4 5
)
[7] => Array
(
[0] => 1
[1] => 2 3
[2] => 4
[3] => 5
)
[8] => Array
(
[0] => 1
[1] => 2 3 4
[2] => 5
)
[9] => Array
(
[0] => 1 2
[1] => 3 4 5
)
[10] => Array
(
[0] => 1 2
[1] => 3
[2] => 4 5
)
[11] => Array
(
[0] => 1 2
[1] => 3
[2] => 4
[3] => 5
)
[12] => Array
(
[0] => 1 2
[1] => 3 4
[2] => 5
)
[13] => Array
(
[0] => 1 2 3
[1] => 4 5
)
[14] => Array
(
[0] => 1 2 3
[1] => 4
[2] => 5
)
[15] => Array
(
[0] => 1 2 3 4
[1] => 5
)
)
Цель
Можно не обязательно так, но предположительно так.
[0] => Array
(
[0] => 1 2 3 4 5
)
[1] => Array
(
[0] => 1
[1] => 2 3 4 5
)
[2] => Array
(
[0] => 1 2 3 4
[1] => 5
)
[3] => Array
(
[0] => 1
[1] => 2
[2] => 3 4 5
)
[4] => Array
(
[0] => 1 2 3
[1] => 4
[2] => 5
)
[5] => Array
(
[0] => 1 2
[1] => 3 4 5
)
[6] => Array
(
[0] => 1 2 3
[1] => 4 5
)
[7] => Array
(
[0] => 1
[1] => 2 3
[2] => 4 5
)
[8] => Array
(
[0] => 1 2
[1] => 3
[2] => 4 5
)
[9] => Array
(
[0] => 1 2
[1] => 3 4
[2] => 5
)
[10] => Array
(
[0] => 1
[1] => 2 3 4
[2] => 5
)
[11] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4 5
)
[12] => Array
(
[0] => 1
[1] => 2
[2] => 3 4
[3] => 5
)
[13] => Array
(
[0] => 1
[1] => 2 3
[2] => 4
[3] => 5
)
[14] => Array
(
[0] => 1 2
[1] => 3
[2] => 4
[3] => 5
)
[15] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)