Hello.

Partially advanced in solving their own problems. Now it makes sense to rephrase the question:

there is an array of three elements: $arr = array("a","b","c")

How is it possible to generate from it unique child arrays with two elements in each?

for a given example, this is:

 $arr_1 = array("a", "b"); $arr_2 = array("a", "c"); $arr_3 = array("b", "c"); 

and

How is it possible to sort an array with three elements reaching all six possible sequences of elements?

for a given example, this is:

 $arr_1 = array("a","b","c"); * $arr_2 = array("a","c","b"); $arr_3 = array("b","a","c"); $arr_4 = array("b","c","a"); $arr_5 = array("c","a","b"); $arr_6 = array("c","b","a"); * 

* getting the source and reverse arrays is not difficult. question in the other four options.

Maybe someone can suggest which way to dig?

There is no need to write a listing, I just need help in drawing up the algorithm.

  • The variant about double arrays is solved. Question in sorting triple array. - ikoolik
  • Thank you all, the question is closed. - ikoolik

2 answers 2

I can offer a solution for an array with three elements:

 $arr = array( 'a', 'b', 'c' ); for ($i = 0; $i < 3; $i++) echo ($arr[$i] . ", "); for ($i = 0; $i < 3; $i++) { $c_arr = $arr; array_splice($c_arr, $i, 1); for ($j = 0; $j < 2; $j++) echo ($arr[$i] . $c_arr[$j] . ", "); } for ($i = 0; $i < 3; $i++) { $c_arr = $arr; array_splice($c_arr, $i, 1); echo ($arr[$i] . $c_arr[0] . $c_arr[1] . ","); echo ($arr[$i] . $c_arr[1] . $c_arr[0] . ","); } 

In fact, everything was not so difficult. For example, my method is to simply rearrange the elements without modifying the original array.

  • that is necessary. - ikoolik
  • This is good =) - AseN

Your option.

  $arr_1 = array("a", "b"); $arr_2 = array("a", "c"); $arr_3 = array("b", "c"); 

My option.

 $arr[0] = array("a", "a"); $arr[0] = array("a", "b"); $arr[0] = array("a", "c"); $arr[0] = array("b", "a"); $arr[0] = array("b", "b"); $arr[0] = array("b", "c"); $arr[0] = array("c", "a"); $arr[0] = array("c", "b"); $arr[0] = array("c", "c"); 

You can also

The meaning is just what?

Usually do as. Take an array of values. Make it another array of all elements, say 4 characters long.

Those.

 $arr = array("a", "b","c","d","e","r" и т.д.); 

4 cycles for nested to make an array. example.

 a b c d . . . . . aaaa aaab aaac aaad . . . . zzzy zzzz и т.д. 

You will have an array with all possible values.

You have such a question

Generate unique strings from elements of a single array.

So why make a bunch of arrays? make one and with it then whatever you want.

  • unfortunately, when editing, I accidentally removed a line from the question stating that the generated strings should not contain duplicate elements. those. ("a", "a") does not roll = (But thanks for the work. - ikoolik
  • It is not so difficult to prescribe the condition that the characters do not repeat in the string. - Artem
  • @Shrek about "why a bunch of arrays?" after generation, arrays are translated into strings with several different separators between the elements, which is already implemented by the function that needs to be fed to the array. - ikoolik
  • And what prevents to make an array of strings in a loop? The implementation is the same. - Artem