0

我不确定在 PHP 中访问二维数组时遇到的错误。基本上我的 var_dump() 给了我以下信息:

 array(1) {
   ['x']=>
     string(1) "3"
 }
 array(1) {
   ['y']=>
     string(3) "3"
 }

 array(1) {
   ['x']=>
     string(1) "5"
 }
 array(1) {
   ['y']=>
     string(3) "5"
 }

var_dump 是正确的,并显示了我想要达到的结果。

我正在做的事情如下:1)在 $points 数组中准备 x 和 y 坐标 2)检查某些数字是否在给定的坐标内:

    function check_collisions {
    $points = array();
    for($y = 0; $y < count($this->Ks); $y++)
    {
        $points[]['x'] = $this->Ks[$y][0]; // first is 3, second is 5 - see var_dump above
        $points[]['y'] = $this->Ks[$y][1]; // first is 3, second is 5 - see var_dump above
    }


    for($p = 0; $p < count($points); $p++)
    {
        for($r = 0; $r < count($this->Ns); $r++)
        {
            if($points[$p]['x'] >= $this->Ns[$r][0] && $points[$p]['x'] <= $this->Ns[$r][2])
            {

                if($points[$p]['y'] >= $this->Ns[$r][1] && $points[$p]['y'] <= $this->Ns[$r][3])
                {

                    $collisions++;
                }
            }
        }
    }
    return $collisions;
    }

我的 PHP 现在告诉我 x 和 y 是两个 if 条件中的未定义索引。有什么不对的吗?其他索引运行良好,例如访问 $this->Ns 等。有什么想法吗?

4

1 回答 1

0

将循环更改for为如下所示:

for($y = 0; $y < count($this->Ks); $y++)
{
    $points[] = array('x' => $this->Ks[$y][0], 'y' => $this->Ks[$y][1]);
}

当您分配给$points[]没有索引时,它每次都会附加到数组中。您将两个数组附加到$points每个循环而不是带有坐标的单个数组。

于 2012-05-25T19:40:23.380 回答