未定义偏移创建/加入多维数组值 [英] Undefined offset when creating/adding Multi-Dimensional array values

查看:241
本文介绍了未定义偏移创建/加入多维数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我增加了动态数组,使用类似于此code;

  $ arrayF [$ F + 1] [$ Y] [$ X + 1] = $值+ 1;

但我收到此错误报告:

未定义抵消:1

问题:为什么我得到一个不确定的,当我试图创建一个数组值的偏移量

我能做些什么呢?

另外的信息,如果相关:
它发生在一个环路所以,我在哪里'蔓延'到一个masterArray

 如果(is_array($ arrayF [$ F])){
    的foreach($ arrayF [$ F]为$键2 => $ arrayF2){
        $ Y = $ KEY2;
        的foreach($ arrayF2为$ KEY3 => $值){
            $ X = $ KEY3;
            如果(($ y的&下; = 100)及及($ y的> = 1)及及($ X&下; = 100)及及($ X> = 1)){
                如果($值< $ arrayMaster [$ Y] [$ X]){
                    $ arrayMaster [$ Y] [$ X] = $价值;主数组中//重置价值
                    $ arrayF [$ F + 1] [$ Y] [$ X + 1] = $值+ 1; //创建F到'扩张'用新阵
                    $最大= $ F + 1;
                }
            }
        }
    }
}


解决方案

简单,因为当你这样做: $ arrayF [$ F + 1] [$ Y] [$ X + 1] = $值+ 1; ,你不能确保 $ arrayF [$ F + 1] 是一个有效的偏移/索引/键。所有你肯定知道的是, is_array($ arrayF [$ F])是真实的。

解决方法是相当简单:

 如果(!使用isset($ arrayF [$ F + 1])||!is_array($ arrayF [$ F + 1])){
    $ arrayF [$ F + 1] =阵列(
        $ Y =>阵列()
    );
}否则如果(!is_array($ arrayF [$ F + 1] [$ Y])){
        $ arrayF [$ F + 1] [$ Y] =阵列();
}
$ arrayF [$ F + 1] [$ Y] [$ X + 1] = $值+ 1;

现在你为什么要得到通知?这是因为,如果 $ arrayF [$ F + 1] 不存在,PHP会很乐意为您创建它,当你做一个简单的分配如 $ arrayF [$ F + 1] ='foobar的'; 。不过,你所访问(可能)不存在的偏移(这将计算结果为),并尝试使用它作为一个数组: $ arrayF [$ F + 1] [$ Y] ,如果 $ arrayF [$ F + 1] 不存在,也可以不会出现在我们那里 $ Y 指数,因此该通知:PHP是警告你在你的code一个可能的错误

更新:的结果
由于在下面的评论中讨论: is_array 可以产生一个未定义的偏移通知,看到它假定您传递给它的说法确实存在。为了避免被生产此类通知,一个使用isset 检查是必需的,所以我已经更新了code以上相应。

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

解决方案

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.

这篇关于未定义偏移创建/加入多维数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆