PHP未定义的偏移数组错误

时间:2017-03-04 09:10:10

标签: php arrays offset

我为这个作业制作了这个数组,但我不能完全正确。我需要它做的是识别我的数组中的字符串并打印出“我正在学习它”。我想我很接近,但我一直在接受未定义的偏移。

 $myArray = array ("C ", "Java", "C++ ", "Objective-C", "C#", "PHP","(Visual)
                   Basic", "Python", "JavaScript", "Perl", "Ruby",
                   "PL/SQL", "Delphi/Oject pascal","Visual Basic.Net",
                   "lisp", "Pascal", "Ada", "Transact-SQL", "Logo", "NXT-G");

for ($i = 0; $i<sizeof($myArray); $i++)
    echo $myArray[$i];

if($myArray[$i] == "C") {
    echo "I'm learning this too!";
}
else {
    echo "Your not learning anything";
}

2 个答案:

答案 0 :(得分:2)

您缺少for loop

的括号
    $myArray = array ("C ", "Java", "C++ ", "Objective-C", "C#", "PHP","(Visual)
                   Basic", "Python", "JavaScript", "Perl", "Ruby",
                  "PL/SQL", "Delphi/Oject pascal","Visual Basic.Net",
                 "lisp", "Pasca", "Ada", "Transact-SQL", "Logo", "NXT-G");

for ($i = 0; $i< sizeof($myArray); $i++)
{
  echo $myArray[$i]."<br>";
  if($myArray[$i] == "C") {
    echo "I'm learning this too!";
  }
  else {
    echo "Your not learning anything";
  }
}

答案 1 :(得分:1)

您在for循环中缺少括号括起来。

for循环多次执行for命令后的单个指令。如果你想做多件事,你需要将它们组合成一个带括号{}

的块
for ($i = 0; $i<sizeof($myArray); $i++)
{
     echo $myArray[$i];
     if($myArray[$i] == "C") 
     {
         echo "I'm learning this too!";
     }
     else
     {
         echo "Your not learning anything";
     }
}

尝试并严格遵守您的积木的布局格式,否则当您的大括号没有正确链接时,您会更难注意到。