简体   繁体   English

注意: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

how to solve this issue the raw array is如何解决这个问题原始数组是

Array ( [1] => 50 [ 2] => 100 [ 3] => 150 [ 4] => 175 [ 5] => 200 [ 6] => 225 [ 7] => 250 ) 

Code:代码:

<?php
    $myfile = fopen("bin\PriceDays.txt", "r") or die("Unable to open file!");
    $string = fread($myfile,filesize("bin\PriceDays.txt"));
    //string to array
    $a = explode(',', $string);

    foreach ($a as $result) {
        $b = explode('. ', $result);
        $PriceDays[$b[0]] = $b[1];
    }

    print_r($PriceDays);
    echo $PriceDays[2];




    fclose($myfile);

?> ?>

The problem is that you have a space after , in your input, so $b[0] will begin with a space.问题是,你有后面输入一个空格,在你的输入,所以$b[0]会以空格开头。 Use trim() to remove surrounding whitespace.使用trim()删除周围的空白。

foreach ($a as $result) {
    $b = explode('. ', $result);
    $PriceDays[trim($b[0])] = trim($b[1]);
}

暂无
暂无

声明:本站的技术帖子网页,遵循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 注意:未定义的偏移量:第19行的C:\\ xampp \\ htdocs \\ h_php \\ addTimes.php中的1 - Notice: Undefined offset: 1 in C:\xampp\htdocs\h_php\addTimes.php on line 19 注意:第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 致命错误:在第 30 行调用 C:\\xampp\\htdocs\\magento\\test.php 中布尔值的成员函数 getChildren()? - Fatal error: Call to a member function getChildren() on boolean in C:\xampp\htdocs\magento\test.php on line 30? 致命错误:在第 16 行调用 C:\\xampp\\htdocs\\studysite\\test.php 中布尔值的成员函数 fetchAll() - Fatal error: Call to a member function fetchAll() on boolean in C:\xampp\htdocs\studysite\test.php on line 16 致命错误:在第2行的C:\\ Apache \\ htdocs \\ test.php中调用未定义的函数mysql_connect() - Fatal error: Call to undefined function mysql_connect() in C:\Apache\htdocs\test.php on line 2 如何在PHP中添加两个变量而没有出现“警告:第28行0在C:\\ xampp \\ htdocs \\ game \\ test.php中遇到的非数值”错误 - How to add two variables in PHP without getting “Warning: A non-numeric value encountered in C:\xampp\htdocs\game\test.php on line 28 0” error 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM