在php中,我编写了一个脚本来计算数组中的元音个数并输出结果。
运行脚本时,出现Undefined offset
错误。
这个密码怎么了?
for($a = 0; $a < count($expld); $a++) {
for($b = 0; $b < strlen($expld[$b]); $b++) {
if ($expld[$b] == 'A' || $expld[$b] == 'a' || $expld[$b] == 'E' || $expld[$b] == 'e'
|| $expld[$b] == 'I' || $expld[$b] == 'i' || $expld[$b] == 'O' || $expld[$b] == 'o'
|| $expld[$b] == 'U' || $expld[$b] == 'u')
{
$vowel++;
}
}
echo "$expld[$a] has $vowel vowels(s).<br> ";
}
最佳答案
看来你把索引搞砸了,看看:
for($a = 0; $a < count($expld); $a++)
{
$vowel = 0;
for($b = 0; $b < strlen($expld[$a]); $b++)
{
if($expld[$a][$b] == 'A' || $expld[$a][$b] == 'a' || $expld[$a][$b] == 'E' || $expld[$a][$b] == 'e'
|| $expld[$a][$b] == 'I' || $expld[$a][$b] == 'i' || $expld[$a][$b] == 'O' || $expld[$a][$b] == 'o'
|| $expld[$a][$b] == 'U' || $expld[$a][$b] == 'u')
{
$vowel++;
}
}
echo "$expld[$a] has $vowel vowels(s).<br> ";
}