Ask for an algorithm for the combination of two-dimensional array elements in PHP

Source: Internet
Author: User
$arr = array(    array('a','b','c'),    array('c','f'),    array('g','z'),    array('x','y'));//$arr子集元素长度可能会多一些//将$arr的子集元素与$arr其他子集元素两两组合或者三三四四组合//子集array('a','b','c')中的元素不需要组合//两两组合$newarr = array(  array('a','c'),  array('a','f'),  array('b','c'),  array('b','f'),  array('c','c'),  array('c','f'),  ……)//三三组合$newarr = array(  array('a','c','g'),  array('a','f','g'),  array('b','c','g'),  array('b','f','g'),  array('c','c','g'),  array('c','f','g'),  ……)//四四组合$newarr = array(  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 it be implemented with a function?

//$arr:原始数组,$cNum:组合长度function getCombination($arr,$cNum){  ……}

Reply content:

$arr = array(    array('a','b','c'),    array('c','f'),    array('g','z'),    array('x','y'));//$arr子集元素长度可能会多一些//将$arr的子集元素与$arr其他子集元素两两组合或者三三四四组合//子集array('a','b','c')中的元素不需要组合//两两组合$newarr = array(  array('a','c'),  array('a','f'),  array('b','c'),  array('b','f'),  array('c','c'),  array('c','f'),  ……)//三三组合$newarr = array(  array('a','c','g'),  array('a','f','g'),  array('b','c','g'),  array('b','f','g'),  array('c','c','g'),  array('c','f','g'),  ……)//四四组合$newarr = array(  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 it be implemented with a function?

//$arr:原始数组,$cNum:组合长度function getCombination($arr,$cNum){  ……}

Recursion?

$arr = array(    array('a', 'b', 'c'),    array('c', 'f'),    array('g', 'z'),    array('x', 'y'));//$arr:原始数组,$cNum:组合长度function getCombination($arr, $cNum) {    if ($cNum === 0) {        return return array(            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));

Uh...... My current idea is to take the array that meets the set length, then the Cartesian product, and finally merge it into a new array ...
Three functions completed.

The problem with the main question is PHP计算二维数组笛卡尔积 , right? If so, take a look at 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, $arrResultCoun            T);            Array_push ($currentArraySlice, $currentArray [$i]);        $this->calcdescartes ($arrIndex + 1, $currentArraySlice); }}} $example = [[' A ', ' B ', ' C '], [' C ', ' F '], [' G ', ' Z '], [' X ', ' Y ']]; $result = []; $descartes = new Descart Es ($example, $result); $descartes->calcdescArtes (0, $result); Var_dump ($descartes->resultarray); 
  • 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.