PHP user-defined functions generate Cartesian product

Source: Internet
Author: User
: This article mainly introduces PHP user-defined functions to generate Cartesian products. For more information about PHP tutorials, see.
 "; Print_r (combineDika ($ color, $ size, $ local);/*** Cartesian product of all arrays ** @ param unknown_type $ data */function combineDika () {$ data = func_get_args (); $ cnt = count ($ data); $ result = array (); foreach ($ data [0] as $ item) {$ result [] = array ($ item) ;}for ($ I = 1; $ I <$ cnt; $ I ++) {$ result = combineArray ($ result, $ data [$ I]);} return $ result;}/*** Cartesian product of two arrays ** @ param unknown_type $ arr1 * @ param unknown_type $ arr2 */f Unction combineArray ($ arr1, $ arr2) {$ result = array (); foreach ($ arr1 as $ item1) {foreach ($ arr2 as $ item2) {$ temp = $ item1; $ temp [] = $ item2; $ result [] = $ temp;} return $ result ;}?>

Running result:

Array(    [0] => Array        (            [0] => red            [1] => 39            [2] => beijing        )    [1] => Array        (            [0] => red            [1] => 39            [2] => shanghai        )    [2] => Array        (            [0] => red            [1] => 40            [2] => beijing        )    [3] => Array        (            [0] => red            [1] => 40            [2] => shanghai        )    [4] => Array        (            [0] => red            [1] => 41            [2] => beijing        )    [5] => Array        (            [0] => red            [1] => 41            [2] => shanghai        )    [6] => Array        (            [0] => green            [1] => 39            [2] => beijing        )    [7] => Array        (            [0] => green            [1] => 39            [2] => shanghai        )    [8] => Array        (            [0] => green            [1] => 40            [2] => beijing        )    [9] => Array        (            [0] => green            [1] => 40            [2] => shanghai        )    [10] => Array        (            [0] => green            [1] => 41            [2] => beijing        )    [11] => Array        (            [0] => green            [1] => 41            [2] => shanghai        ))


The above introduces the PHP user-defined function to generate Cartesian products, including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.