PHP implements Cartesian product algorithm
Concept
In mathematics, the Cartesian product (Cartesian product) of two sets X and Y, also known as the direct product, is expressed as X × Y. Suppose A and B are any two sets. Take any element x from set A and take any element y from set B to form an ordered pair (x, y). Take such an ordered pair as a new Elements, the set composed of all of them is called the direct product of set A and set B, denoted as A×B, that is, A×B={(x, y)|x∈A and y∈B}.
Assume that set A={a, b} and 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)}.
Example
Given three domains:
D1 = { 张清玫,刘逸 } D2 = {计算机专业,信息专业} D3 = {李勇,刘晨,王敏}
Then the Cartesian product D of D1, D2, D3 = D1×D2×D3, Equal to:
{ (张清玫, 计算机专业, 李勇), (张清玫, 计算机专业, 刘晨), (张清玫, 计算机专业, 王敏), (张清玫, 信息专业, 李勇), (张清玫, 信息专业, 刘晨), (张清玫, 信息专业, 王敏), (刘逸, 计算机专业, 李勇), (刘逸, 计算机专业, 刘晨), (刘逸, 计算机专业, 王敏), (刘逸, 信息专业, 李勇), (刘逸, 信息专业, 刘晨), (刘逸, 信息专业, 王敏) }
In this way, each element in the three sets D1, D2, and D3 is combined correspondingly to form a huge set group. In this example, there will be 2X2X3=12 elements in D. If a set has 1000 elements and there are 3 such sets, the new set composed of their Cartesian product will reach one billion elements. If a set is infinite, then the new set will have infinite elements.
PHP code - Output array form
function Descartes() { $t = func_get_args(); // 获取传入的参数 if (func_num_args() == 1) { // 判断参数个数是否为1 return call_user_func_array(__FUNCTION__, $t[0]); // 回调当前函数,并把第一个数组作为参数传入 } $a = array_shift($t); // 将 $t 中的第一个元素移动到 $a 中,$t 中索引值重新排序 if ( !is_array($a)) { $a = [$a]; } $a = array_chunk($a, 1); // 分割数组 $a ,为每个单元1个元素的新数组 do { $r = []; $b = array_shift($t); if ( !is_array($b)) { $b = [$b]; } foreach ($a as $p) { foreach (array_chunk($b, 1) as $q) { $r[] = array_merge($p, $q); } } $a = $r; } while ($t); return $r; }
Usage:
$arr = [ [ '张清玫', '刘逸' ], [ '计算机专业', '信息管理与信息系统专业', '电子商务专业' ], [ '2018级', '2017级' ] ]; $r = Descartes($arr);
Effect:
array(12) { [0]=> array(3) { [0]=> string(9) "张清玫" [1]=> string(15) "计算机专业" [2]=> string(7) "2018级" } [1]=> array(3) { [0]=> string(9) "张清玫" [1]=> string(15) "计算机专业" [2]=> string(7) "2017级" } [2]=> array(3) { [0]=> string(9) "张清玫" [1]=> string(33) "信息管理与信息系统专业" [2]=> string(7) "2018级" } [3]=> array(3) { [0]=> string(9) "张清玫" [1]=> string(33) "信息管理与信息系统专业" [2]=> string(7) "2017级" } [4]=> array(3) { [0]=> string(9) "张清玫" [1]=> string(18) "电子商务专业" [2]=> string(7) "2018级" } [5]=> array(3) { [0]=> string(9) "张清玫" [1]=> string(18) "电子商务专业" [2]=> string(7) "2017级" } [6]=> array(3) { [0]=> string(6) "刘逸" [1]=> string(15) "计算机专业" [2]=> string(7) "2018级" } [7]=> array(3) { [0]=> string(6) "刘逸" [1]=> string(15) "计算机专业" [2]=> string(7) "2017级" } [8]=> array(3) { [0]=> string(6) "刘逸" [1]=> string(33) "信息管理与信息系统专业" [2]=> string(7) "2018级" } [9]=> array(3) { [0]=> string(6) "刘逸" [1]=> string(33) "信息管理与信息系统专业" [2]=> string(7) "2017级" } [10]=> array(3) { [0]=> string(6) "刘逸" [1]=> string(18) "电子商务专业" [2]=> string(7) "2018级" } [11]=> array(3) { [0]=> string(6) "刘逸" [1]=> string(18) "电子商务专业" [2]=> string(7) "2017级" } }
The above is the detailed content of PHP implements Cartesian product algorithm. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
