如何解决此问题的原始数组是
Array ( [1] => 50 [ 2] => 100 [ 3] => 150 [ 4] => 175 [ 5] => 200 [ 6] => 225 [ 7] => 250 )
代码:
<?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);
?>
答案 0 :(得分:0)
问题是输入中,
后面有一个空格,因此$b[0]
将以空格开头。使用trim()
删除周围的空白。
foreach ($a as $result) {
$b = explode('. ', $result);
$PriceDays[trim($b[0])] = trim($b[1]);
}