2012-04-23 2 views
0

클래스에 선언 된 배열을 임의 순서로 섞을 수 없습니다.shuffle 배열이 클래스 PHP에서 비어 있습니다.

나에게 제공 : 빈 속성에 액세스 할 수 없습니다. 나는 체스 판을 전시하고 싶다. 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(); 
+0

첫 번째 히트에서와 같이 variable property에 액세스하는 것은 정확하게 문제처럼 보이는, 저에게이 준 : http://php.syntaxerrors.info/index.php?title=Cannot_access_empty_property –

답변

3

, 그것은 $this->board 대신 $this->$board해야한다. 두 번째 형태는 구글에

$propertyName = 'board'; 
shuffle($this->$propertyName); 
관련 문제