[英]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.