Can {code...} be implemented using a function? {Code ...}
$ Arr = array ('A', 'B', 'C'), array ('C', 'F'), array ('G ', 'Z'), array ('x', 'y ')); /// the length of the $ arr subset element may be more. // combine the subset element of $ arr with the other subset elements of $ arr, or combine them with three, three, four, and four. // The subset array ('A', elements in 'B', 'C') do not need to be combined // two or two combinations $ newarr = array ('A', 'C'), array ('A ', 'F'), array ('B', 'C'), array ('B', 'F'), array ('C', 'C '), array ('C', 'F '),......) // Three combinations $ newarr = array ('A', 'C', 'G'), array ('A', 'F', 'G '), array ('B', 'C', 'G'), array ('B', 'F', 'G'), array ('C', 'C ', 'G'), array ('C', 'F', 'G '),......) // Four combinations $ newarr = array ('A', 'C', 'G', 'x'), array ('A', 'F ', 'G', 'x'), array ('B', 'C', 'G', 'x'), array ('B', 'F ', 'G', 'x'), array ('C', 'C', 'G', 'x'), array ('C', 'F ', 'G', 'x '),......)
Can I use a function?
// $ Arr: original array, $ cNum: combination length function getCombination ($ arr, $ cNum ){......}
Reply content:
$ Arr = array ('A', 'B', 'C'), array ('C', 'F'), array ('G ', 'Z'), array ('x', 'y ')); /// the length of the $ arr subset element may be more. // combine the subset element of $ arr with the other subset elements of $ arr, or combine them with three, three, four, and four. // The subset array ('A', elements in 'B', 'C') do not need to be combined // two or two combinations $ newarr = array ('A', 'C'), array ('A ', 'F'), array ('B', 'C'), array ('B', 'F'), array ('C', 'C '), array ('C', 'F '),......) // Three combinations $ newarr = array ('A', 'C', 'G'), array ('A', 'F', 'G '), array ('B', 'C', 'G'), array ('B', 'F', 'G'), array ('C', 'C ', 'G'), array ('C', 'F', 'G '),......) // Four combinations $ newarr = array ('A', 'C', 'G', 'x'), array ('A', 'F ', 'G', 'x'), array ('B', 'C', 'G', 'x'), array ('B', 'F ', 'G', 'x'), array ('C', 'C', 'G', 'x'), array ('C', 'F ', 'G', 'x '),......)
Can I use a function?
// $ Arr: original array, $ cNum: combination length function getCombination ($ arr, $ cNum ){......}
Recursion?
$ Arr = array ('A', 'B', 'C'), array ('C', 'F'), array ('G ', 'Z'), array ('x', 'y'); // $ arr: original array, $ cNum: combined length function getCombination ($ arr, $ cNum) {if ($ cNum = 0) {return array ('A'), array ('B'), array ('C '),);} else {$ tmpArr2 = $ arr; $ resultArr = array (); array_pop ($ tmpArr2); $ lastNewArr = getCombination ($ tmpArr2, $ cNum-1 ); for ($ I = 0; $ I <count ($ lastNewArr); $ I ++) {for ($ j = 0; $ j <count ($ arr [$ cNum]); $ j ++) {$ tmpArr = $ lastNewArr [$ I]; $ tmpArr [] = $ arr [$ cNum] [$ j]; $ resultArr [] = $ tmpArr ;}return $ resultArr ;}} print_r (getCombination ($ arr, count ($ arr)-1 ));
Er ...... My current idea is to first retrieve the array that complies with the length of the combination, then calculate the Cartesian product, and then merge it into a new array ......
Three functions are completed.
The question of the subject should bePHP calculates the Cartesian product of a two-dimensional arrayRight? If yes, see the following code:
class Descartes{ public $sourceArray; public $resultArray; public function __construct($array, $result) { $this->sourceArray = $array; $this->resultArray = $result; } public function calcDescartes($arrIndex, $arrResult) { if ($arrIndex >= count($this->sourceArray)) { array_push($this->resultArray, $arrResult); return ; } $currentArray = $this->sourceArray[$arrIndex]; $currentArrayCount = count($currentArray); $arrResultCount = count($arrResult); for ($i = 0; $i < $currentArrayCount; ++$i) { $currentArraySlice = array_slice($arrResult, 0, $arrResultCount); array_push($currentArraySlice, $currentArray[$i]); $this->calcDescartes($arrIndex + 1, $currentArraySlice); } }}$example = [ ['a', 'b', 'c'], ['c', 'f'], ['g', 'z'], ['x', 'y']];$result = [];$descartes = new Descartes($example, $result);$descartes->calcDescartes(0, $result);var_dump($descartes->resultArray);