How to calculate the Cartesian product of multiple sets with PHP

Source: Internet
Author: User
Cartesian productIn mathematics, the Cartesian product of two sets X and Y (Cartesian product), also called the direct product, is represented as X*y, the first object is the member of X and the second object is one of all possible ordered pairs of Y.

Suppose set a={a,b}, set b={0,1,2}, the Cartesian product of two sets is {(a,0), (a,1) , (a,2), (b,0), (b,1), (b,2)}

idea: the Cartesian product of the first and second sets is first computed, and the result is saved as a new set.
The Cartesian product is then computed with the new set and the next set, depending on the loop until the Cartesian product is computed with the last set.

For example, there are several sets that need to be computed for the Cartesian product

<?php$sets = Array (    ' white ', ' black ', ' red '), array ('    breathable ', ' anti-skid '),    Array (' 37 yards ', ' 38 yards ', ' 39 yards '),    Array ( ' Male ', ' female '); >

The code is as follows:

<?php/** * PHP calculates the Cartesian product of multiple collections * Date:    2017-01-10 * Author:  fdipzone * Ver:     1.0 * * Func * cartesianproduct COMPUTE multiple The Cartesian product of a set *//** * computes the Cartesian product of multiple sets * @param array  $sets set of arrays * @return array */function cartesianproduct ($sets) {    //Save Results    $result = Array ();    Iterate through the collection data    for ($i =0, $count =count ($sets), $i < $count-1; $i + +) {        //Initialize        if ($i ==0) {            $result = $sets [ $I];        }        Save temporary data        $tmp = Array ();        The result is computed with the next set of Cartesian product        foreach ($result as $res) {            foreach ($sets [$i +1] as $set) {                $tmp [] = $res. $set;            }        }        //writes the Cartesian product to the result        $result = $tmp;    }    return $result;} Define the collection $sets = Array (    ' white ', ' black ', ' red '), array ('    breathable ', ' anti-skid '),    Array (' 37 yards ', ' 38 yards ', ' 39 yards ')    , Array (' Male ', ' female '); $result = Cartesianproduct ($sets);p rint_r ($result);? >

Output:

Array (    [0] = white breathable 37 yards male    [1] = white breathable 37 yards female    [2] = white breathable 38 yards male    [3] = white breathable 38 yards female    [4] = White breathable 39 yards male    [5] = white breathable 39 yards female    [6] = white non-slip 37 yards Mens    [7] = white non-slip 37 yards female    [8] = white non-slip 38 yards Mens    [9] = White non-slip 38 yards female    [ten] = white non-slip 39 yards Mens    [one] = white non-slip 39 yards women    [] = black breathable 37 yards men    [] = black breathable 37 yards women    [+] = Black breathable 38 yards mens    [38] + black breathable women's [+    ] = Black breathable 39 yards men    [+] = Black breathable 39 yards women    [] = black non-slip 37 yards male    [+] = Black non-slip 37 yards female    [38] = black non-slip 38 yards men's    [+] = black non-slip size of women    [[] = black non-slip 39 yards mens    [] = black non-slip 39 yards female    [+] + red breathable 37 yards mens    [+] + red breathable 37 yards women    [+] + red breathable 38 yards mens [+    ] = Red breathable 38 yards women's [] ~ +    Red breathable 39 yards mens    [] = Red breathable 39 yards women's [37] and    red non-slip 37 yards of men    [~] and red anti-skid yards women    [38] and red anti-slip 38 yards of men    [] and red anti-slip yards women    Red anti-slip 39 yards Mens    [+] + red non-slip 39 yards female)

This article explains how to calculate the Cartesian product of multiple sets through PHP, and more about the PHP Chinese web.

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.