Relating items of an array recursively with PHP

Question:

I would like to know if there is any native PHP function to relate all items of arrays with all items of other arrays, 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 everyone has related, and the initial arrays can vary in number as well as their contents.

Answer:

I didn't 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;

Scroll to Top