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"
}
The var_dump is imho correct and shows the results I wanted to achieve.
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?
Change your
forloop to look like this:When you assign to
$points[]with no index it appends to the array every time. You are appending two arrays to$pointseach loop instead of a single array with the coordinates.