如何按顺序列出多维数组的元素

时间:2018-02-02 14:01:39

标签: php arrays multidimensional-array

我有这个二维数组:

$poss = array( array("W","W"), array("L","L"), array("L","W"), array("W","L") );

我希望数组$ poss的每个元素都列在每12行深的列中,一个元素可以在一列中重复。像这样的东西(一个样本专栏)

W,W
L.L
L.W
W,L
W,W
L.L
L.W
W,L
W,W
L.L
L.W
W,L

我写了下面的代码,但它没有给出所需的结果。

$poss = array( array("W","W"), array("L","L"), array("L","W"), array("W","L") );

for($x = 0; $x < 4; $x++)   
{
    for($y = 0; $y < 2; $y++)
    {
        echo $poss[$x][$y];
        echo "<br>";
    }
}

4 个答案:

答案 0 :(得分:1)

您可以执行类似

的操作
$poss = array( array("W","W"), array("L","L"), array("L","W"), array("W","L") );

for ( $x = 0; $x < 3; $x++ ) {
    foreach( $poss as $value ) {
        echo implode("," ,$value ). "<br />";
    }
}

这将导致:

W,W
L,L
L,W
W,L
W,W
L,L
L,W
W,L
W,W
L,L
L,W
W,L

<强>更新

包含数组元素的所有各种可能组合的12行列

foreach( $poss as $key1 => $value1 ) {
    foreach( $poss as $key2 => $value2 ) {
        if ( $key1 != $key2 ) echo $value1[0] . "," . $value2[1] . "<br />";
    }
}

这将导致:

W,L
W,W
W,L
L,W
L,W
L,L
L,W
L,L
L,L
W,W
W,L
W,W

答案 1 :(得分:1)

<强>更新 您可以使用位逻辑来生成此类表(从此blog post获取的基本方法):

$columns = [];
for ($i = 0; $i < 4096; $i++) {
    $column = str_replace(
        ['0', '1'],
        ['L', 'W'],
        str_pad(decbin($i), 12, '0', STR_PAD_LEFT)
    );

    echo $column, PHP_EOL;

    $columns[] = str_split($column);
}

这是the demo

您可以使用pc_next_permutation生成所有排列,然后使用array_chunk将其划分为列:

function pc_next_permutation($p, $size) {
    // slide down the array looking for where we're smaller than the next guy
    for ($i = $size - 1; $p[$i] >= $p[$i+1]; --$i) { }

    // if this doesn't occur, we've finished our permutations
    // the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
    if ($i == -1) { return false; }

    // slide down the array looking for a bigger number than what we found before
    for ($j = $size; $p[$j] <= $p[$i]; --$j) { }

    // swap them
    $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;

    // now reverse the elements in between by swapping the ends
    for (++$i, $j = $size; $i < $j; ++$i, --$j) {
         $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;
    }

    return $p;
}

$set = array_map(function ($a) {
    return implode(',', $a);
}, array( array("W","W"), array("L","L"), array("L","W"), array("W","L") ));
$size = count($set) - 1;
$perm = range(0, $size);
$j = 0;

do { 
     foreach ($perm as $i) { $perms[] = $set[$i]; }
} while ($perm = pc_next_permutation($perm, $size) and ++$j);

array_map(function (...$row) {
    echo implode(' | ', $row), PHP_EOL;
}, ...array_chunk($perms, 12));

这将为您提供输出:

W,W | W,W | L,L | L,L | L,W | L,W | W,L | W,L
L,L | L,W | W,W | L,W | W,W | L,L | W,W | L,L
L,W | W,L | L,W | W,L | L,L | W,L | L,L | L,W
W,L | L,L | W,L | W,W | W,L | W,W | L,W | W,W
W,W | W,W | L,L | L,L | L,W | L,W | W,L | W,L
L,L | W,L | W,W | W,L | W,W | W,L | W,W | L,W
W,L | L,L | W,L | W,W | W,L | W,W | L,W | W,W
L,W | L,W | L,W | L,W | L,L | L,L | L,L | L,L
W,W | W,W | L,L | L,L | L,W | L,W | W,L | W,L
L,W | W,L | L,W | W,L | L,L | W,L | L,L | L,W
L,L | L,W | W,W | L,W | W,W | L,L | W,W | L,L
W,L | L,L | W,L | W,W | W,L | W,W | L,W | W,W

这是the demo

答案 2 :(得分:0)

尽量保持简单:

implode()

从Eddie复制{{1}}因为这是个好主意。 ; - )

答案 3 :(得分:0)

    $poss = array( array("W","W"), array("L","L"), array("L","W"), array("W","L") );

for ($i=0; $i<3; $i++){
      foreach ($poss as $item ){
          if(is_array($item)){
             foreach ($item as $value){
               echo $value;
        }
        echo "<br>";
        }
}
    }