我有一个名为“product.txt”的文本文件,其中包含一些产品:
Porsche Bike^A bike with a brand name!^10,000
Pretty Shoes^Come to our shoe store^54.45
Pie Fest!^Oh yeah this is officially the best pie ever^3.45
Inside Out Umbrella^Designer Umbrellas for low cost =^14.55
Coffee^Come get your morning dessert^4.59
我想将它们放入一个带有函数的数组中:
function loadFile() {
$filename = ('product.txt');
$file = fopen($filename, 'r');
while (!feof($file)) {
$line = trim(fgets($file));
return $array = explode("^", $line);
}
fclose($file);
}
我的功能问题是它不会将所有产品都放入数组中,而只放在第一行!
答案 0 :(得分:1)
将您的功能更改为
function loadFile() {
$line = array();
$filename = ('product.txt');
$file = fopen($filename, 'r');
while (!feof($file)) {
$line[] = trim(fgets($file));
}
fclose($file);
return $line;
}