完整数组的未定义偏移量但 vardump 已满

undefined offset of full array but vardump is full

我的 PHP 代码有一些问题。

<?php
  if(!empty($_POST['IP_adress'])){
    $IP_adress = $_POST['IP_adress'];
    $block[4] = explode(".",$IP_adress);
    $i = 0;
    var_dump($block);
    for($i=0;$i<4;$i++){
      if ($block[$i]<0 || $block[$i]>255){//here is line 9
        $_SESSION['IP_error'] = "non-valid IP adress";
        header('Location: controle.php');
      }
    }
  }
?>

当我启动代码时,var_dump 函数说我的数组 ($block) 已正确填充,但我有这个错误。

array(1) { [4]=> array(4) { [0]=> string(3) "255" [1]=> string(3) "255" [2]=> string(3) "255" [3]=> string(3) "255" } }

Notice: Undefined offset: 0 in /var/www/html/select.php on line 9

Notice: Undefined offset: 0 in /var/www/html/select.php on line 9

Notice: Undefined offset: 1 in /var/www/html/select.php on line 9

Notice: Undefined offset: 1 in /var/www/html/select.php on line 9

Notice: Undefined offset: 2 in /var/www/html/select.php on line 9

Notice: Undefined offset: 2 in /var/www/html/select.php on line 9

Notice: Undefined offset: 3 in /var/www/html/select.php on line 9

Notice: Undefined offset: 3 in /var/www/html/select.php on line 9

[output errors][1]

你能帮帮我吗?

p.s。抱歉语法和英语不好。这里以法语为母语!

第 5 行 中,您使用了 $block[4] = explode(".",$IP_adress);,这就是 var_dump 的输出告诉您的内容。

您已经创建了一个关联数组,其中您的分解 IP 数组 存储为数组 $block 的键 4 处的值。要访问展开的 IP 字段,您可以将 行 #5 修改为 $block = explode(".",$IP_adress); 或将 行 #9 修改为 if ($block[4][$i]<0 || $block[4][$i]>255){