I have the following code: (Apologies I’m not sure how to put it in a code block!)

$n = 4; # Number of N through input
$squares = $n * $n; # Max number of squares
$Position = 1; # Position of queen number one
$Column = $Position + $n; # Formula for finding column
$i = 1;
$key = 0;
while ($i <= $n){ $spaces["Row[$key]"] = array(); $i++; $key++;} # creates an array with 8 arrays with a key of Row
array_push($spaces["Row[0]"], $Position);
print_r($spaces);
$key = 1; # moves the array key on to enter results from first iteration into row 2 of the array
$limit = 200;
While ($limit > 0)
{# 1
if($Position == $Column) { $Column = $Column + $n; }
else { array_push($spaces["Row[$key]"], $Position); echo "array Entry: $Position <br><br>"; } # puts remaining available spaces in an array
$Position++;
if ($Position == $squares + 1) {$key++; $Position = 1; }
$limit--;
} # 1
echo "<br><br>out of loop "; print_r($spaces); echo "<br><br>";

which generates the quoted warning. I believe that the $Position = 1 of the latter if statement is somehow to blame though I don’t understand why. If I delete this, or use ignore code the warning isn’t generated. The $key++ is working but the $Position isn’t. Erm, any ideas as to why or where to look for further info…

I’m running PHP version 7.3.9, if that makes any difference.

Hi, thanks for answering. Forgive my ignorance however I don’t understand how the array can accept $position (as an integer) for the first iterations but when $position value is changed in the second if loop, again to an integer, it chucks a wobbler…

PHP declares parameters in 1-index. Parameter 1 is the first parameter. Array_push expects an array in parameter 1, and then mixed variables in optional parameters 2…MAX_INT.

Debugging time… trace your variables.

By the time we get to the first array_push,
$Squares is 16, $Position is 1; $Column is 5; $key is 4. $spaces contains 4 keys - Row[0] Row[1] Row[2], and Row[3].
We push 1 into the empty array Row[0].
We then reset key to 1.
Limit is 200, which is greater than 0.
Position does not equal Column,
We push 1 into $spaces[“Row[1]”]
We set Position to 2
Position does not equal to 17.
We set Limit to 199
Restart Loop
Repeat until Position (Code pauses at line $Position++;)is 17. Limit is now 185. Position now equals squares+1, key becomes 2. Position becomes 1.
Repeat until Position (Code pauses at line $Position++;)is 17. Limit is now 169. Position now equals squares+1, key becomes 3. Position becomes 1.
Repeat until Position (Code pauses at line $Position++;)is 17. Limit is now 153. Position now equals squares+1, key becomes 4. Position becomes 1.
Limit gets decremented to 152.
Restart loop.
Position does not equal column.
Code tries to array_push($spaces["Row[$key]"], $Position);
Key is 4. “Row[4]” is not a valid key in $spaces. Your first parameter therefore is NULL.
Abort. Fail.

Reevaluate your algorithm. You’ve made a calculation error somewhere. What are you trying to do.

Yeah, you’re right. The Key value is incrementing too far and the script is asking to push something into an array row that doesn’t exist. Thanks for pointing me in the right direction, v much appreciated…