2013-02-23 53 views
0

嘿我有简单的循环即时尝试创建ArrayObject,以便它拥有一个坐标系统。具有多个值的ArrayObjects?

我试图通过在X坐标中放置Y坐标以减少重复数据来减少数据量。

这是我的尝试:

$object = new ArrayObject(); 
$xWidth=1; 
$yWidth=2; 

for ($x=0; $x < $xWidth; $x++) { 
     $object[$x] = new ArrayObject(); 

    for ($y=0; $y < $yWidth; $y++) { 
     $object[$x][$y]; 
    } 
} 

的问题是数据出来不出我所料...这是我看到的数据:

ArrayObject Object 
(
    [storage:ArrayObject:private] => Array 
     (
      [0] => ArrayObject Object 
       (
        [storage:ArrayObject:private] => Array 
         (
         ) 

       ) 

     ) 

) 

任何想法如何我可以将第二个Y数字放入X ArrayObject中?

+2

'$对象[$ X] [$ Y];'没有做任何事情。 – 2013-02-23 01:40:31

+3

更改'$对象[$ x] [$ y];'到'$对象[$ x] [] = $ y;' – alfasin 2013-02-23 01:41:00

+0

@WaleedKhan ........我知道这就是为什么我问这个问题。 .. – Sir 2013-02-23 01:41:03

回答

1

你为什么不只是使用的对象,包括Y和X

  class coordinates{ 

      public __constructor($x, $y){ 
      $this->x = $x; 
       $this->y = $y; 
       } 
       private $x; 
      private $y; 
      public function setX($x){ 
      $this->x = $x; 
      } 

      public function setY($y){ 
      $this->y = $y; 
      } 

      public function getX(){ 
      return $this->x; 
      } 

      public function getY(){ 
      return $this->y; 
      } 

      } 

      $cordinate = new coordinates($x, $y); 

      $collectionCordinates[] = $cordinate; 
+0

感谢您的意见:) – Sir 2013-02-23 01:59:08