不能洗牌在类中声明的数组。
它给了我:无法访问空属性。我想展示一个棋盘。使用 2D 阵列。如果不用作类中的函数,则代码工作正常。我想洗牌数组并显示它。
private $board = array(array('k', 'k', 'b', 'q', 'k', 'b', 'k', 'r'),
array('p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'),
array(' ', ' ', ' ', ' ', ' ',
' ', ' ', ' '),
array('p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'),
array('r', 'k', 'b', 'q', 'k', 'b', 'k', 'r'));
public function produce()
{
shuffle($this->$board);
echo "<div id='inside'>";
for($yIndex = 0; $yIndex < count($board); $yIndex++)
{
echo "<div class='row'>";
for($xIndex = 0; $xIndex < count($board[$yIndex]); $xIndex++)
{
echo "<div class='column' data-square=$yIndex-$xIndex>
<div class=".$board[$yIndex][$xIndex]."></div></div>";
}
echo "</div>";
}
}}$a = new Display();$a->produce();
你的语法错误,它应该是$this->board
而不是$this->$board
。第二种形式是访问变量属性,如
$propertyName = 'board';
shuffle($this->$propertyName);