简体   繁体   English

注意:未定义的偏移量:第19行的C:\\ xampp \\ htdocs \\ h_php \\ addTimes.php中的1

[英]Notice: Undefined offset: 1 in C:\xampp\htdocs\h_php\addTimes.php on line 19

With the below code I have a problem where I'm getting the 1st and 2nd row just fine, but the 3rd and next rows only give this error: 使用下面的代码,我遇到了一个问题,我无法正确地获得第一行和第二行,但是第三行和下一行仅给出此错误:

Notice: Undefined offset: 1 in C:\\xampp\\htdocs\\h_php\\addTimes.php on line 19. 注意:第19行的C:\\ xampp \\ htdocs \\ h_php \\ addTimes.php中未定义的偏移量:1。

<?php
$timearry="";
$timearry=array("1:10","1:40","1:20","0:50");
$i=0;
$day1hours="";
foreach($timearry as $times){
    if($i==0){
        echo $day1hours= $times;
        echo "<br>";
    }else{
        $day2hours = $times;
        $day1=array();
        $day1 = explode(":", $day1hours);
        $day2 = explode(":", $day2hours);
        $totalmins = 0;
        $totalmins += $day1[0] * 60;
        $totalmins += $day1[1];
        $totalmins += $day2[0] * 60;
        $totalmins += $day2[1];
        $hoursTotal = $totalmins / 60;
        $hours=0;
        $hours = explode(".", $hoursTotal);
        $hours= $hours[0];
        $minutes = $totalmins % 60;
        echo $day1hours = "$hours".'Hours '."$minutes".' Mints';
        echo "<br>";
    }
    $i++;
}
?>

According to your logic, 根据您的逻辑,

Here is the mistake 这是错误

echo $day1hours = "$hours" . 'Hours ' . "$minutes" . ' Mints';

This line should be 这行应该是

echo $day1hours = $hours . ':'.$minutes;

Output: 输出:

1:10
2:50
4:10
5:0

See demo here 在此处查看演示

That's not an error, it's just a notice, telling you that on the 19th line of your code you're using an offset that doesn't exist. 这不是一个错误,只是一个通知,告诉您在代码的第19行上,您使用的偏移量不存在。

Assuming the code you posted is complete, this means this line is not working properly: 假设您发布的代码完整,这意味着此行无法正常工作:

$totalmins += $day2[1];

because $day2 array doesn't seem to have 2 elements. 因为$day2数组似乎没有2个元素。

Did you check if all the elements from $timearry are correctly formatted as "H:m"? 您是否检查了$timearry中的所有元素$timearry都正确设置为“ H:m”?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 注意:未定义偏移量:1 in C:\\xampp\\htdocs\\index.php 第 5 行 - Notice: Undefined offset: 1 in C:\xampp\htdocs\index.php on line 5 注意:第21行的C:\\ xampp \\ htdocs \\ isan \\ hasildata.php中的未定义偏移量:0 - Notice: Undefined offset: 0 in C:\xampp\htdocs\isan\hasildata.php on line 21 数组([&#39;1&#39;] =&gt; 1)1注意:未定义的偏移量:第152行的C:\\ xampp \\ htdocs \\ HR \\ functions \\ functions_applicants.php中的1 - Array ( ['1'] => 1 ) 1 Notice: Undefined offset: 1 in C:\xampp\htdocs\HR\functions\functions_applicants.php on line 152 2 注意:Undefined offset: 2 in C:\\xampp\\htdocs\\test.php on line 20 如何解决 - Notice: Undefined offset: 2 in C:\xampp\htdocs\test.php on line 20 how to solve 注意:第19行的C:\\ xampp \\ htdocs \\ tes1.php中的数组到字符串的转换 - Notice: Array to string conversion in C:\xampp\htdocs\tes1.php on line 19 PHP:注意:未定义的索引:第9行的C:\\ xampp \\ htdocs \\ vdab \\ cookies.php中的NameFilledIn - PHP : Notice: Undefined index: NameFilledIn in C:\xampp\htdocs\vdab\cookies.php on line 9 php注意:未定义的索引:第3行的C:\\ xampp \\ htdocs \\ includes \\ middle.php中的id - php Notice: Undefined index: id in C:\xampp\htdocs\includes\middle.php on line 3 PHP 注意:未定义索引:第 30 行 C:\\xampp\\htdocs\\try1.php 中的步骤 - PHP Notice: Undefined index: step in C:\xampp\htdocs\try1.php on line 30 注意:未定义的索引:第6行的C:\\ xampp \\ htdocs \\ project \\ uploads \\ upload.php中的文件 - Notice: Undefined index: file in C:\xampp\htdocs\project\uploads\upload.php on line 6 注意:未定义的变量:第18行的C:\\ xampp \\ htdocs \\ mvc \\ app \\ validation \\ DForm.php中的currentValue - Notice: Undefined variable: currentValue in C:\xampp\htdocs\mvc\app\validation\DForm.php on line 18
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM