$ mix - array combines arrays with words separated by spaces. array_unique in this case does not work and the words are duplicated at the output.

$mix = array_merge($arr_mix_1, $arr_mix_2, $arr_mix_3); $mixed = array_unique($mix); shuffle($mixed); $result = implode(' ', $mixed); 

Well, I know that how the textbook works. And my code does not work. It was necessary to immediately lay out the entire code for evaluation.

 <?php $rand_a = rand(700,900); $rand_b = rand(500,700); $rand_c = rand(30,40); $arr_1 = explode(' ', file_get_contents('data/big_file.txt')); shuffle($arr_1); $a = -1; while (++$a <= $rand_a) { $arr_mix_1[] = $arr_1[$a]; } $arr_2 = explode(' ', file_get_contents('data/key_file.txt')); shuffle($arr_2); $b = -1; while (++$b <= $rand_b) { $arr_mix_2[] = $arr_2[$b]; } $arr_3 = explode(' ', file_get_contents('data/must_have_file.txt')); shuffle($arr_3); $c = -1; while (++$c <= $rand_c) { $arr_mix_3[] = $arr_3[$c]; } $mix = array_merge($arr_mix_1, $arr_mix_2, $arr_mix_3); $mixed = array_unique($mix); shuffle($mixed); $result = implode(' ', $mixed); ?> 
  • Everything works correctly: watch the demo . - Edward
  • most likely an error due to non-displayable characters (carriage return and end of the string "\ r \ n") Well, also, you have a lot of redundant code. - Edward

1 answer 1

Try deleting newline characters. And I also reduced your code a bit:

 $arr_mix_1 = get_contents('data/big_file.txt', 700, 900); $arr_mix_2 = get_contents('data/key_file.txt', 500, 700); $arr_mix_3 = get_contents('data/must_have_file.txt', 30, 40); $mixed = array_unique(array_merge($arr_mix_1, $arr_mix_2, $arr_mix_3)); shuffle($mixed); $result = join(' ', $mixed); echo $result; function get_contents($file, $min, $max) { $rand = rand($min, $max); $content = file_get_contents($file); $words = explode(' ', str_replace(PHP_EOL, ' ', $content)); shuffle($words); $result = []; $a = -1; while (++$a <= $rand) { $result[] = $words[$a]; } return $result; }