Relate items in an array recursively with PHP

3

I would like to know if there is any native function of PHP to relate all the items of arrays with all the items of other arrays, for example:

$arr['a'] = [1,2,3];
$arr['b'] = [4,5];
$arr['c'] = [7,8,9,10];

To get the following result:

$result[] = [1,4,7];
$result[] = [1,4,8];
$result[] = [1,4,9];
$result[] = [1,4,10];
$result[] = [1,5,7];
$result[] = [1,5,8];

And so on, until all have been related, and the initial arrays can vary in number as well as their contents.

    
asked by anonymous 30.10.2017 / 19:27

1 answer

2

I did not find anything native but I solved it this way, it worked for me!

    $arr_ovp = [ 'Cor'      => ['White','Black','Green','Blue','Red'],
                 'Tamanho'  => ['G','XG','XXL','XXG'],
                 'Manga'    => ['Curta','Cumprida','SemManga'],
                 'Estampa'  => ['Breaking Bad','Back to the Future','Star Wars']];



foreach($arr_ovp as $variacoes) {

    if( empty($arr1) ) {
        foreach($variacoes as $variacao) {
            $arr1[] = $variacao;
        }
    } else {
        foreach($arr1 as $opcao) {
            foreach($variacoes as $variacao) {
                $arr2[] = $opcao.' - '.$variacao;
            }
        }
        $arr1 = $arr2;
        $arr2 = '';
    }
}
echo "<pre>";
print_r($arr1);
exit;
    
01.11.2017 / 16:23