1

如果我有 6 个数组,并且每个数组有 6 个值(不是零),并且我想测试一个方程,同时假设它们中的 3 个数组在每个循环中都是零值,我该怎么做?

例子:

我有这 6 个数组:

$row_1 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_2 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_3 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_4 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_5 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_6 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);

循环 1:

$row_1 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_2 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_3 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_4 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_5 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_6 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);

循环 2:

$row_1 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_2 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_3 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_4 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_5 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_6 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);

循环 3:

$row_1 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_2 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_3 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_4 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_5 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_6 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);

循环 4:

$row_1 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_2 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_3 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_4 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_5 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_6 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);

循环 5:

$row_1 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_2 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_3 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_4 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_5 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_6 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);

循环 6:

$row_1 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_2 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_3 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_4 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_5 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_6 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);

循环 7:

$row_1 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_2 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_3 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_4 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_5 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_6 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);

循环 8:

$row_1 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_2 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_3 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_4 = array(a => 0, b => 0, c => 0, d => 0, e => 0, f => 0);
$row_5 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);
$row_6 = array(a => 1, b => 2, c => 3, d => 4, e => 5, f => 6);

………………

循环 n:

…………

4

2 回答 2

0

我不明白这样做的目的,但也许是这样的?

我没有使用 $row1、$row2、$row3 等,而是做了一个数组。Array 在这种情况下使用起来更加灵活。我做了一个示例,说明如何将当前变量输入到一个数组中。

我已经更改了代码,现在正在使用不同的方法。我使用一个对象来存储数组,然后在循环的每次迭代中使用对象中存储的数组来“重置”。(无论如何,OOP 是最好的做事方式 :-))

<?php
//Create a class to handle array
class arrayHandler {
    private $row = array();

    public function __construct(Array $row) {
        $this->row = $row; //Put array into object member
    }   

    public function loopArray($num=7, $numRandomEntries=3) {

        for ($nr = 0;$nr<intval($num);$nr++) { //Do seven loops

            //Get array stored in object
            $row = $this->row;

            //Get three random keys from the $row-array
            $randomEntries = array_rand($row, intval($numRandomEntries));

            //Set random entries elements to value 0
            foreach($randomEntries as $key) {
                $row[$key] = array_fill_keys(range('a', 'f'), 0);
            }

            $nNr = $nr+1;
            echo "row$nNr = " . print_r($row,true)."<hr />"; //For testing

        }

    }

}

//Your variables with stored arrays...
$row1 = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6);
$row2 = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6);
$row3 = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6);
$row4 = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6);
$row5 = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6);
$row6 = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6);

//Create ONE array from each variable above
$row = array();
$row[0] = $row1;
$row[1] = $row2;
$row[2] = $row3;
$row[3] = $row4;
$row[4] = $row5;
$row[5] = $row6;

//Create the object and do the looping
$ah = new arrayHandler($row);
$ah->loopArray();
?>
于 2013-05-11T07:08:51.453 回答
0

您基本上想要“值|无值”的所有组合。有 64 种不同的组合 ( 2^6)。获取两个值的所有组合的一个简单技巧是使用一个整数,将其递增 1 以获得下一个组合,然后检查其位模式:

$origArray = array(
  array_combine(range('a', 'f'), range(1, 6)),
  array_combine(range('a', 'f'), range(1, 6)),
  array_combine(range('a', 'f'), range(1, 6)),

  array_combine(range('a', 'f'), range(1, 6)),
  array_combine(range('a', 'f'), range(1, 6)),
  array_combine(range('a', 'f'), range(1, 6)),
);
$emptyArray = array_fill_keys(range('a', 'f'), 0);

define('COMBINATION_LENGTH', 6);

for($combination = pow(2, COMBINATION_LENGTH)-1; $combination >= 0; --$combination) {
  $currentCombinationArray = array();

  for($bitPosition = 0; $bitPosition < COMBINATION_LENGTH; ++$bitPosition) {
    $includeArray = ($combination >> (COMBINATION_LENGTH - $bitPosition - 1)) & 1;

    $currentCombinationArray[$bitPosition] = $includeArray ? $origArray[$bitPosition] : $emptyArray;
  }

  var_dump($currentCombinationArray);
}

这行得通,但我不确定它的性能如何。

于 2013-05-11T07:56:03.090 回答