PHP for for循环中的循环.尝试对多个数组列求和以获得组合
发布时间:2020-12-13 16:16:08 所属栏目:PHP教程 来源:网络整理
导读:我试图循环一个二维数组并自动采取列的组合总和. 假设我有一个名为$a的数组,有4列:0,1,2,3, $a=array();$a[0][0]=1;$a[0][1]=3;$a[0][2]=5;$a[1][0]=10;$a[1][1]=2; $a[1][2]=5;$a[1][3]=7;$a[2][0]=9;$a[2][1]=8; $a[2][2]=9;$a[2][3]=8;$a[3][0]=9;$a[3][
我试图循环一个二维数组并自动采取列的组合总和.
假设我有一个名为$a的数组,有4列:0,1,2,3, $a=array(); $a[0][0]=1; $a[0][1]=3; $a[0][2]=5; $a[1][0]=10; $a[1][1]=2; $a[1][2]=5; $a[1][3]=7; $a[2][0]=9; $a[2][1]=8; $a[2][2]=9; $a[2][3]=8; $a[3][0]=9; $a[3][1]=8; $a[3][2]=9; $a[3][3]=8; $a[3][4]=1; 我试图使用此代码总结列的所有组合,如sum(0,0; ??1,0; 2; 0,3; 0)等 for($i=0;$i<count($a[0]);$i++){ for($l=0;$l<count($a[1]);$l++){ for($s=0;$s<count($a[2]);$s++){ for($m=0;$m<count($a[3]);$m++){ echo $sum[]= $a[0][$i]+$a[1][$l]+$a[2][$s]+$a[3][$m]; echo $sum; echo "<br>"; } } } } ?> 并且代码工作,问题是我正在手动执行这些循环,必须有一些方法,我可以通过以某种方式插入列数的数量来简化这个? 我试过类似的东西 $numberofcolumns=4; for($n=0;$n<$numberofcolumns;$n++){ for($i=0;$i<count($a[$n]);$i++){ for($m=0;$m<count($a[$n+1]);$m++){ echo $sums[]= $a[$n][$i]+$a[$n+1][$m]; } } } 但这不起作用,必须有一些方法来简化for循环,这样我就不必手动输入每个列的for循环 有人有头绪吗? 解决方法
您可以使用递归或直接嵌套循环,但是当使用组合或排列时,可能性的总数可能会爆炸并变成一个巨大的数字,消耗大量内存到您无法运行代码的程度.使用迭代器是交换cpu效率以提高内存效率的好方法.这是我写的迭代器.
class CartesianProductIterator implements Iterator { protected $iterators; function __construct(array $iters) { $this->iterators = $iters; } function rewind() { foreach ($this->iterators as $it) { $it->rewind(); } } function current() { $values = array(); foreach ($this->iterators as $it) { $values[] = $it->current(); } return $values; } function key() { return null; } function next() { /* loop them in reverse,but exclude first why? example,odometer: 55199 you always check the rightmost digit first to see if incrementing it would roll it over and need to be "rewound" to 0,which causes the digit to the left to increase as well,which may also cause it to roll over as well,and so on... looping in reverse operates from right column to the left. we dont rewind the first column because if the leftmost column is on its last element and needs to roll over then this iterator has reached its end,and so rewind() needs to be explicitly called */ for ($i = count($this->iterators) - 1; $i > 0; --$i) { $it = $this->iterators[$i]; $it->next(); if ($it->valid()) { // were done advancing because we found a column that didnt roll over return; } else { $it->rewind(); } } //if execution reached here,then all of the columns have rolled over,so we must attempt to roll over the left most column $this->iterators[0]->next(); } function valid() { return $this->iterators[0]->valid(); } } 然后用它作为 $iterators = array(); foreach ($a as $columnNumber => $values) { $iterators[] = new ArrayIterator($values); } foreach (new CartesianProductIterator($iterators) as $combo) { // combo has 1 value from each of the ArrayIterators we instantiated printf("summing %s = %dn",join('+',$combo),array_sum($combo)); } 继续演示http://codepad.org/UasdgvWf (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |