English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

A Detailed Explanation of Calculating Cartesian Product of Multiple Sets in PHP

Cartesian product

In mathematics, the Cartesian product of two sets X and Y, also known as the direct product, is denoted by X*Y, the first object is a member of X and the second object is one of the possible ordered pairs of Y.

Assuming set A={a,b}, set B={0,1,2}), then the Cartesian product of the two sets is {(a,0),(a,1),(a,2),(b,0),(b,1),(b,2})

Implementation ideas

First, calculate the Cartesian product of the first set and the second set, and save the result as a new set.

Then calculate the Cartesian product of the new set with the next set, and so on, until the Cartesian product with the last set is calculated.

For example, there are the following sets that need to calculate the Cartesian product

<?php
$sets = array(
 array('White', 'Black', 'Red'),
 array('Breathable', 'Non-slip'),
 array('37Code',38Code',39Code',
 array('Male', 'Female')
);
?>

The code is as follows:

<?php
/**
 * php calculate the Cartesian product of multiple sets
 * Date: 2017-01-10
 * Author: fdipzone
 * Ver: 1.0
 *
 * Func
 * CartesianProduct calculates the Cartesian product of multiple sets
 */
/**
 * calculate the Cartesian product of multiple sets
 * @param Array $sets Set array
 * @return Array
 */
function CartesianProduct($sets){
 // save the result
 $result = array();
 // loop through the set data
 for($i=0,$count=count($sets); $i<$count-1; $i++{
 // Initialization
 if($i==0){
  $result = $sets[$i];
 }
 // Save Temporary Data
 $tmp = array();
 // Calculate Cartesian Product with the Next Set
 foreach($result as $res){
  foreach($sets[$i+1] as $set){
  $tmp[] = $res.$set;
  }
 }
 // Write Cartesian Product into Result
 $result = $tmp;
 }
 return $result;
}
// Define Set
$sets = array(
 array('White', 'Black', 'Red'),
 array('Breathable', 'Non-slip'),
 array('37Code',38Code',39Code',
 array('Male', 'Female')
);
$result = CartesianProduct($sets);
print_r($result);
?>

Output:

Array
(
 [0] => White Breathable37Male Model
 [1=> White Breathable37Female Model
 [2=> White Breathable38Male Model
 [3=> White Breathable38Female Model
 [4=> White Breathable39Male Model
 [5=> White Breathable39Female Model
 [6=> White Non-slip37Male Model
 [7=> White Non-slip37Female Model
 [8=> White Non-slip38Male Model
 [9=> White Non-slip38Female Model
 [10=> White Non-slip39Male Model
 [11=> White Non-slip39Female Model
 [12=> Black Breathable37Male Model
 [13=> Black Breathable37Female Model
 [14=> Black Breathable38Male Model
 [15=> Black Breathable38Female Model
 [16=> Black Breathable39Male Model
 [17=> Black Breathable39Female Model
 [18=> Black Non-slip37Male Model
 [19=> Black Non-slip37Female Model
 [20] => Black Non-slip38Male Model
 [21=> Black Non-slip38Female Model
 [22=> Black Non-slip39Male Model
 [23=> Black Non-slip39Female Model
 [24=> Red Breathable37Male Model
 [25=> Red Breathable37Female Model
 [26=> Red Breathable38Male Model
 [27=> Red Breathable38Female Model
 [28=> Red Breathable39Male Model
 [29=> Red Breathable39Female Model
 [30] => Red Non-slip37Male Model
 [31=> Red Non-slip37Female Model
 [32=> Red Non-slip38Male Model
 [33=> Red Non-slip38Female Model
 [34=> Red Non-slip39Male Model
 [35=> Red Non-slip39Female Model
)

Summary

That's all about using PHP to calculate the Cartesian product of multiple sets. I hope the content of this article can bring some help to everyone's learning or using PHP. If you have any questions, you can leave a message for communication.

Statement: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been manually edited, and does not bear relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like