Mystery Undefined offset 0 warning after if loop array_shift()

Hi All. After smashing my head on the edge of the desk for a day or two I’ve finally admitted defeat and decided to ask for help.

I have the following array (part of a larger array) which is identified as: $spaces[“Row[$key]”] - $key being “3” in this example.

[Row[3]] => Array ( [0] => 26 [1] => 31 [2] => 32 [3] => 34 [4] => 36 [5] => 40 [6] => 44 [7] => 50 [8] => 52 [9] => 54 [10] => 58 [11] => 60 [12] => 62 [13] => 63 )

out of which I’m using a loop part of which is to extract the following data:

$last = end($spaces[“Row[$key]”]);
$squares = $last;
list($first) = $spaces[“Row[$key]”];
$Position = $first;

all that works fine and dandy however when I use the following:

$Dump = array_shift($spaces[“Row[$key]”]);

in an if subloop to drop the 26 from the beginning giving me

[Row[3]] => Array ( [0] => 31 [1] => 32 [2] => 34 [3] => 36 [4] => 40 [5] => 44 [6] => 50 [7] => 52 [8] => 54 [9] => 58 [10] => 60 [11] => 62 [12] => 63 )

and then the:

$last = end($spaces[“Row[$key]”]); * In this case 63 which is working and correct
$squares = $last;
list($first) = $spaces[“Row[$key]”];
$Position = $first;

again, which works in the loops before, the list() instruction throws a “PHP Notice: Undefined offset: 0” warning despite there clearly being something in the array row $key = 3

Frankly, I’m baffled. I understand, roughly what the warning means however I’m pretty sure there’s a value where the code is being instructed to look. I’ve tried a reset() just in case the keys were out of whack, but it made no difference.

Any help, advice, suggestions would be v much appreciated…

Well, welcome to the forums. I’d advise you use your fingers rather than your forehead to do the typing, though. :wink:

mmmhkay. That’s an interesting choice for an array key, but you do you.

First of all, let me throw out an administrative bit: to put code blocks into your post, surround them with three backticks.

```
code here
```

I don’t… really understand what the idea of this code block is.
$last gets set to the last value of the array.
$squares also gets set to the last value of the array.
$first gets set to either the last value of the array (PHP5) or the first (PHP7).
$Position gets set to the same value as $first.
you then set $Dump to the first value of the array (getting rid of it in the process)
Seems like the whole thing could be done easier with

$squares = end($spaces["Row[$key]"]);
$Position = reset($spaces["Row[$key]"]);

or even set $Position with the array_shift. OR use the loop index to access the array instead of shortening it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.