There is a line Один Два Три ... n

We need to get at the output: (in general, all possible combinations за исключением ОдинОдин, ДваДва, ТриТри, ОдинОдинОдин, ДваДваДва,ТриТриТри )

 Один Два Три ОдинДва ОдинТри ДваОдин ДваТри ТриОдин ТриДва ОдинДваТри ОдинТриДва ДваОдинТри ДваТриОдин ТриОдинДва ТриДваОдин 

Code

 $str = "один два три"; $split_str = explode(' ', $str); echo "<pre>"; print_r($split_str); echo "</pre>"; 
  • and 113 for example? - teran
  • @teran Looks like a "modified" factorial. Typically done by either recursion or cycles. Or "combinatorial task for non-repeating combinations" - nick_n_a

2 answers 2

 $array = []; $array[1] = $main_array = ['one', 'two', 'three']; $size = count($array[1]); $arr_name = 1; for ($count = 2; $count <= $size; $count++) { $this_size = count($array[$arr_name]); $arr_name++; for ($i = 0; $i < $this_size; $i++) { $gg = $array[($arr_name-1)][$i]; for ($x = 0; $x < $size; $x++) { if($gg !== $array[1][$x]) { $array[$arr_name][] = $gg . $array[1][$x]; } } } if (!empty($main_array)) { $main_array = array_merge($main_array, $array[$arr_name]); } if ($arr_name > 2) unset($array[($arr_name-1)]); } unset($array[$arr_name]); print_r($main_array); 

    In PHP it is not strong, but you can use the regular program. Something like:

     $str = "один два три"; $split_str = explode(' ', $str); echo "<pre>"; foreach($split_str as $value) { if (preg_match('^([А-Я][а-я]+)\1+$', $value, $match)) { print_r($value); } } echo "</pre>"; 
    • There are two problems. One get an array. Secondly remove duplicates. Perhaps the second you managed. - nick_n_a
    • $ split_str = explode ('', $ str) - isn't it getting an array? - JavaJunior pm
    • And you need an array with combinations. it's more than just an array. This type of array "multiply" the array. Type 3 in 3rd degree (27) +/- appendages. Combinatorics - tedious. - nick_n_a
    • And if more precisely here 3 ^ 1 + 3 ^ 2 + 3 ^ 3 - doubles. - nick_n_a
    • Anyway, I did not understand the task. Helped what I could - JavaJunior