$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'),……)用一个函数可以实现吗?//$arr:原始数组,$cNum:组合长度functiongetCombination($arr,$cNum){……}
2 回答
慕村9548890
TA贡献1884条经验 获得超4个赞
递归?$arr=array(array('a','b','c'),array('c','f'),array('g','z'),array('x','y'));//$arr:原始数组,$cNum:组合长度functiongetCombination($arr,$cNum){if($cNum===0){returnreturnarray(array('a'),array('b'),array('c'),);}else{$tmpArr2=$arr;$resultArr=array();array_pop($tmpArr2);$lastNewArr=getCombination($tmpArr2,$cNum-1);for($i=0;$ifor($j=0;$j $tmpArr=$lastNewArr[$i]; $tmpArr[]=$arr[$cNum][$j];$resultArr[]=$tmpArr;}}return$resultArr;}}print_r(getCombination($arr,count($arr)-1));
喵喵时光机
TA贡献1846条经验 获得超7个赞
题主的问题应该是PHP计算二维数组笛卡尔积吧?如果是的话,请看以下代码:classDescartes{public$sourceArray;public$resultArray;publicfunction__construct($array,$result){$this->sourceArray=$array;$this->resultArray=$result;}publicfunctioncalcDescartes($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=newDescartes($example,$result);$descartes->calcDescartes(0,$result);var_dump($descartes->resultArray);
添加回答
举报
0/150
提交
取消