0

Hi I ran into a problem that I thought would be easy, but I am not getting what I am looking for.

I have to find all possible combinations for 2 variables that can have 2 values:

X = 1, l Y = e, é

result = aXbcXYaXaY

I must get all possible combinations of aXbcXYaXaY.

Try something like this:

function array_cartesian_product($arrays){
    $result = array();
    $arrays = array_values($arrays);
    $sizeIn = sizeof($arrays);
    $size = $sizeIn > 0 ? 1 : 0;
    foreach ($arrays as $array)
        $size = $size * sizeof($array);
    for ($i = 0; $i < $size; $i++) {
        $result[$i] = array();
        for ($j = 0; $j < $sizeIn; $j++)
            array_push($result[$i], current($arrays[$j]));
        for ($j = ($sizeIn - 1); $j >= 0; $j--) {
            if (next($arrays[$j]))
                break;
            elseif (isset($arrays[$j]))
                reset($arrays[$j]);
        }
    }
    return $result;
}
///// a#bc#éa#aé

$a = array('l', '1');
$b = array('e', 'é');

$result = array($a, $b);
$array_cartesian = array_cartesian_product($result);
//var_dump($array_cartesian);
echo '<br>';echo '<br>';

foreach ($array_cartesian as $subItem) {
    //var_dump($subItem);
    $result2[] = 'a' . $subItem[0] . 'bc' . $subItem[0] . $subItem[1] . 'a' . $subItem[0] . 'a' . $subItem[1] . '<br>';
}

var_dump($result2);

And the result was (CANNOT BE POSSIBLE):

array(4) { 
[0]=> string(14) "albclealae"
[1]=> string(16) "albcléalaé" 
[2]=> string(14) "a1bc1ea1ae"
[3]=> string(16) "a1bc1éa1aé" 
}

Things like combining e with é would be missing because the combination l with 1 seems to be the correct one.

Thank You.

0 Answers0