Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php “如何修复”;“未定义的偏移量”;错误_Php_Arrays - Fatal编程技术网

Php “如何修复”;“未定义的偏移量”;错误

Php “如何修复”;“未定义的偏移量”;错误,php,arrays,Php,Arrays,我试图创建一个简单的平均值计算器,使用随机生成的数字数组。我认为代码非常可靠,但返回的错误如下: Notice: Undefined offset: 10 in ../average/averageresults.php on line 31 第31行: for ($i=0; $i<=10; $i++) { echo $array[$i]."<br />"; } $i=0;$i的 您“被1关闭”。数组有10个元素,0、1、2、3、4、5、6、7、8和9。没有10

我试图创建一个简单的平均值计算器,使用随机生成的数字数组。我认为代码非常可靠,但返回的错误如下:

Notice: Undefined offset: 10 in ../average/averageresults.php on line 31
第31行:

for ($i=0; $i<=10; $i++)   {   echo $array[$i]."<br />";   }
$i=0;$i的

您“被1关闭”。数组有10个元素,0、1、2、3、4、5、6、7、8和9。没有10

将for循环更改为:

对于($i=0;$i您“关闭1”。数组有10个元素,0、1、2、3、4、5、6、7、8和9。没有10

将for循环更改为:


对于($i=0;$iAs是事实,使用
作为事实,使用

<?php
$array = array();

    $array[]=rand(1, 99);
    $array[]=rand(1, 99);
    $array[]=rand(1, 99);
    $array[]=rand(1, 99);
    $array[]=rand(1, 99);
    $array[]=rand(1, 99);
    $array[]=rand(1, 99);
    $array[]=rand(1, 99);
    $array[]=rand(1, 99);
    $array[]=rand(1, 99);

$i=0;
$sum = array_sum($array);
$count = count($array);
$avg = $sum/$count;

for ($i=0; $i<=10; $i++)
  {
  echo $array[$i]."<br />";
  }
echo "The average of these numbers is: ".$avg;

 ?>