[Solved] Make all combination from 3 text file in PHP


Patrick, I’m not sure this is the best way to do this, it does what you want but again I’m not really sure if using 3 consecutive loops is good practice. In real world environment, your txts files may contain hundreds or thousands of entries wich will make this painful for your server to process, you can have memory problems.
Having said this there is my approach, note that I’ve tested this with different number of lines in each text content to make sure it works.
Based on this perhaps you can start another question, maybe someone with more php skills than me can help you improve this.
Last but not least, always remember that when asking you must show us your code, your attempts to reach your goals.

<?php

    function trim_value(&$value) {
        $value = trim($value);
    }
    function explode_text_files($file) {
        $pieces = explode("\n", $file);
        array_walk($pieces, 'trim_value'); // removes white space
        return array_filter($pieces); // deletes empty array entries
    }
    function join_lines($word) {
        return " ".$word;
    }

    $text_from_file_1 = "
    katofle
    bigos
    testing
    ";
    $text_from_file_2 = "
    sa
    nie sa
    ";
    $text_from_file_3 = "
    dobre
    zajebiste
    abc
    zde
    ghjklj
    ";

    $content_from_file_1 = explode_text_files($text_from_file_1);
    $content_from_file_2 = explode_text_files($text_from_file_2);
    $content_from_file_3 = explode_text_files($text_from_file_3);

    $result = "";

    foreach ($content_from_file_1 as $anchor) {
        foreach ($content_from_file_2 as $anchor_2) {
            foreach ($content_from_file_3 as $anchor_3) {
                $result .= $anchor . join_lines($anchor_2) . join_lines($anchor_3);
                $result .= "<br>";
            }
        }
    }

    echo $result;

?>

4

solved Make all combination from 3 text file in PHP