English 中文(简体)
访问两个维数组 PHP
原标题:Accessing two dimensional arrays PHP
  • 时间:2012-05-25 19:35:11
  •  标签:
  • php
  • arrays

I m not sure about an error I m getting while accessing a two-dimensional array in PHP. Basically my var_dump() gives me the following:

 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是不正确的, 并展示了我想要实现的结果。

What I m doing is the following: 1) preparing x and y coordinates within an $points array 2) check if some numbers are within the coordinates given:

    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;
    }

My PHP now tells me that x and y are undefined indexes within the two if conditions. Is there anything wrong? The other indexes are working well, like accessing $this->Ns etc. Any ideas?

最佳回答

更改 环的 , 使其看起来像 :

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

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

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...