A two-dimensional array element combination algorithm in php

Source: Internet
Author: User
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);

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.