Home > Backend Development > PHP Tutorial > 请教一个php中二维数组元素组合的算法

请教一个php中二维数组元素组合的算法

WBOY
Release: 2016-06-06 20:27:34
Original
1491 people have browsed it

<code>$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'),
  ……
)</code>
Copy after login
Copy after login

用一个函数可以实现吗?

<code>//$arr:原始数组,$cNum:组合长度
function getCombination($arr,$cNum){
  ……
}</code>
Copy after login
Copy after login

回复内容:

<code>$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'),
  ……
)</code>
Copy after login
Copy after login

用一个函数可以实现吗?

<code>//$arr:原始数组,$cNum:组合长度
function getCombination($arr,$cNum){
  ……
}</code>
Copy after login
Copy after login

递归?

<code class="php">$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 </code>
Copy after login

呃……我目前的想法是先取符合组合长度规定的数组,再求笛卡尔积,最后合并成新数组……
三个函数完成。

题主的问题应该是 PHP计算二维数组笛卡尔积 吧?如果是的话,请看以下代码:

<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 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);
</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template