How to modify the classic Cartesian Product Code

Source: Internet
Author: User
How can I modify the classic Cartesian product code? Cartesian product code: & lt ;? PHPfunctionDescartes () {& nbsp; $ t = func_get_args (); & nbsp; if (func_num_args () = classic Cartesian product code, how can I modify it?
Cartesian product code:


Function Descartes (){
$ T = func_get_args ();
If (func_num_args () = 1) return call_user_func_array (_ FUNCTION __, $ t [0]);
$ A = array_shift ($ t );
If (! Is_array ($ a) $ a = array ($ );
$ A = array_chunk ($ a, 1 );
Do {
$ R = array ();
$ B = array_shift ($ t );
If (! Is_array ($ B) $ B = array ($ B );
Foreach ($ a as $ p)
Foreach (array_chunk ($ B, 1) as $ q)
$ R [] = array_merge ($ p, $ q );
$ A = $ r;
} While ($ t );
Return $ r;
}

$ Arr = array (
Array ('A1', 'A2 '),
'B ',
Array ('C1 ', 'C2 '),
Array ('d1 ', 'D2', 'd3 '),
Array ('e1 ', 'E2', 'E3 ')
// ...... Omitting other elements (or arrays ),
);

$ R = Descartes ($ arr );
Print_r ($ r );
?>

//////////////////////////////////////

Output:

Array
(
[0] => Array
(
[0] => a1
[1] => B
[2] => c1
[3] => d1
[4] => e1
)
[1] => Array
(
[0] => a1
[1] => B
[2] => c1
[3] => d1
[4] => e2
)
......
)


////////////////////
Now I only want three elements. The first element must have a1 or a2. How can I change the code?
Array
(
[0] => Array
(
[0] => a1
[1] => B
[2] => c1
)
......
[1] => Array
(
[0] => a2
[1] => c2
[2] => d3
)
......
)



------ Solution --------------------
For
$ Arr = array (
Array ('A1', 'A2 '),
'B ',
Array ('C1 ', 'C2 '),
Array ('d1 ', 'D2', 'd3 '),
Array ('e1 ', 'E2', 'E3 ')
// ...... Omitting other elements (or arrays ),
);
You must first
$ Arr = array (
'B ',
Array ('C1 ', 'C2 '),
Array ('d1 ', 'D2', 'd3 '),
Array ('e1 ', 'E2', 'E3 ')
// ...... Omitting other elements (or arrays ),
);
Evaluate the combination of M and 2
$ C = combination ($ arr, 2 );
Then the Cartesian product is obtained for the result of the combination.

$ R = array ();
Foreach ($ c as $ v)
$ R = array_merge ($ r, Descartes (array ('A1', 'A2 '), $ v [0], $ v [1]);
Print_r ($ r );
PHP code
Array(    [0] => Array        (            [0] => a1            [1] => d1            [2] => e1        )    [1] => Array        (            [0] => a1            [1] => d1            [2] => e2        )    [2] => Array        (            [0] => a1            [1] => d1            [2] => e3        )    [3] => Array        (            [0] => a1            [1] => d2            [2] => e1        )    [4] => Array        (            [0] => a1            [1] => d2            [2] => e2        )    [5] => Array        (            [0] => a1            [1] => d2            [2] => e3        )    [6] => Array        (            [0] => a1            [1] => d3            [2] => e1        )    [7] => Array        (            [0] => a1            [1] => d3            [2] => e2        )    [8] => Array        (            [0] => a1            [1] => d3            [2] => e3        )    [9] => Array        (            [0] => a2            [1] => d1            [2] => e1        )    [10] => Array        (            [0] => a2            [1] => d1            [2] => e2        )    [11] => Array        (            [0] => a2            [1] => d1            [2] => e3        )    [12] => Array        (            [0] => a2            [1] => d2            [2] => e1        )    [13] => Array        (            [0] => a2            [1] => d2            [2] => e2        )    [14] => Array        (            [0] => a2            [1] => d2            [2] => e3        )    [15] => Array        (            [0] => a2            [1] => d3            [2] => e1        )    [16] => Array        (            [0] => a2            [1] => d3            [2] => e2        )    [17] => Array        (            [0] => a2            [1] => d3            [2] => e3        )    [18] => Array        (            [0] => a1            [1] => c1            [2] => e1        )    [19] => Array        (            [0] => a1            [1] => c1            [2] => e2        )    [20] => Array        (            [0] => a1            [1] => c1            [2] => e3        )    [21] => Array        (            [0] => a1            [1] => c2            [2] => e1        )    [22] => Array        (            [0] => a1            [1] => c2            [2] => e2        )    [23] => Array        (            [0] => a1            [1] => c2            [2] => e3        )    [24] => Array        (            [0] => a2            [1] => c1            [2] => e1        )    [25] => Array        (            [0] => a2            [1] => c1            [2] => e2        )    [26] => Array        (            [0] => a2            [1] => c1            [2] => e3        )    [27] => Array        (            [0] => a2            [1] => c2            [2] => e1        )    [28] => Array        (            [0] => a2            [1] => c2            [2] => e2        )    [29] => Array        (            [0] => a2            [1] => c2            [2] => e3        )    [30] => Array        (            [0] => a1            [1] => c1            [2] => d1        )    [31] => Array        (            [0] => a1            [1] => c1            [2] => d2        )    [32] => Array        (            [0] => a1            [1] => c1            [2] => d3        )    [33] => Array        (            [0] => a1            [1] => c2            [2] => d1        )    [34] => Array        (            [0] => a1            [1] => c2            [2] => d2        )    [35] => Array        (            [0] => a1            [1] => c2            [2] => d3        )    [36] => Array        (            [0] => a2            [1] => c1            [2] => d1        )    [37] => Array        (            [0] => a2            [1] => c1            [2] => d2        )    [38] => Array        (            [0] => a2            [1] => c1            [2] => d3        )    [39] => Array        (            [0] => a2            [1] => c2            [2] => d1        )    [40] => Array        (            [0] => a2            [1] => c2            [2] => d2        )    [41] => Array        (            [0] => a2            [1] => c2            [2] => d3        )    [42] => Array        (            [0] => a1            [1] => b            [2] => e1        )    [43] => Array        (            [0] => a1            [1] => b            [2] => e2        )    [44] => Array        (            [0] => a1            [1] => b            [2] => e3        )    [45] => Array        (            [0] => a2            [1] => b            [2] => e1        )    [46] => Array        (            [0] => a2            [1] => b            [2] => e2        )    [47] => Array        (            [0] => a2            [1] => b            [2] => e3        )    [48] => Array        (            [0] => a1            [1] => b            [2] => d1        )    [49] => Array        (            [0] => a1            [1] => b            [2] => d2        )    [50] => Array        (            [0] => a1            [1] => b            [2] => d3        )    [51] => Array        (            [0] => a2            [1] => b            [2] => d1        )    [52] => Array        (            [0] => a2            [1] => b            [2] => d2        )    [53] => Array        (            [0] => a2            [1] => b            [2] => d3        )    [54] => Array        (            [0] => a1            [1] => b            [2] => c1        )    [55] => Array        (            [0] => a1            [1] => b            [2] => c2        )    [56] => Array        (            [0] => a2            [1] => b            [2] => c1        )    [57] => Array        (            [0] => a2            [1] => b            [2] => c2        ))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.