1

I am adding to an array on the fly, using code similar to this;

 $arrayF[$f+1][$y][$x+1] = $value+1;

But I receive this in the error report:

Undefined offset: 1

QUESTION: Why do I get an undefined offset when I am trying to CREATE an array value?

What can I do about it?

Further info, if relevant: It occurs in a loop as so, where I am 'sprawling' through a masterArray

if (is_array($arrayF[$f])){ 
    foreach ($arrayF[$f] as  $key2 => $arrayF2) {
        $y = $key2;
        foreach ($arrayF2 as $key3 =>$value) {
            $x = $key3;
            if (($y<=100)&& ($y>=1)&&($x<=100)&&($x>=1)){
                if ($value < $arrayMaster[$y][$x])  {
                    $arrayMaster[$y][$x] = $value;//resets value in a master array
                    $arrayF[$f+1][$y][$x+1] = $value+1;//creates a new array for F to 'sprawl' with
                    $max = $f+1;
                }
            }
        }
    }
}
3
  • have you tried specify that is an array with $arrayF = new array()? it can help maybe Commented Dec 18, 2014 at 10:53
  • @Marco, no it hasn-t been set, could definitely try. Thanks Commented Dec 18, 2014 at 10:55
  • @MarcoMura: arrays don't require the new keyword. They never have, and never will and for this code to even get in the loop $arrayF has to be an array, or the outer if would evaluate to false, so initializing $arrayF won't make any difference Commented Dec 18, 2014 at 11:10

1 Answer 1

2

Simple, because when you do this: $arrayF[$f+1][$y][$x+1] = $value+1;, you can't be sure that $arrayF[$f+1] is a valid offset/index/key. All you know for sure is that is_array($arrayF[$f]) is true.

The fix is rather simple:

if (!isset($arrayF[$f+1]) || !is_array($arrayF[$f+1])) {
    $arrayF[$f+1] = array(
        $y => array()
    );
} else if (!is_array($arrayF[$f+1][$y])) {
        $arrayF[$f+1][$y] = array();
}
$arrayF[$f+1][$y][$x+1] = $value+1;

Now why are you getting the notice? That's because, if $arrayF[$f+1] doesn't exist, PHP will happily create it for you when you do a simple assignment like $arrayF[$f+1] = 'foobar';. However, you're accessing a (possibly) non-existent offset (which would evaluate to null), and try to use it as an array: $arrayF[$f+1][$y], if $arrayF[$f+1] doesn't exist, there can't be a $y index in there, hence the notice: PHP is warning you about a possible bug in your code

Update:
As discussed in the comments below: is_array can produce an undefined offset notice, seeing as it assumes that the argument you pass to it actually exists. To avoid such notices from being produced, an isset check is required, so I've updated the code above accordingly.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, So I would have to prepopulate ALL values of a multi-dimensional arrays until level dimensions-1?
@Gamemorize: Then leave out the if, and just write $arrayF[$f+1] = array($y => array($x+1 => $value+1));, but be weary: in that case, you're bound to re-assign $arrayF[$f+1] endlessly, effectively making your loops redundant
Thx Elias, I'll have a go at your first suggestions, which effectively dynamically declares then assigns. Should be back 'shortly' to accept answer. Thx again!
think of $array[$i] as saying "give me value $i in variable $array, and do something with it", if you want $array[$i][$j], then think of it as ($array[$i])[$j] (take the value of $array[$i], then give me index $j of that value). So in order to add a value 3 levels deep, you have to make sure that all the arrays exist: the main variable must be an array ($arrayF), the offset you use must resolve to an array ($f+1) and the offset in there ($y) must be an array, too. Only then can you assign safely (create $x+1, and assign a value). Hence the checks are required
It seems to be that is_array($array[$x]) will throw undefined offset but isset($array[$x]) does not. Is this the case as expected?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.