我正在学习如何使用 PHP。我将文件内容读入数组并为数组中的每个索引分配变量名。
例如:
$words = file("example.txt"); #each line of the file will have the format a, b, c , d
foreach ($words in $word) {
$content = explode(",", $word); #split a, b, c, d
list($a, $b, $c, $d) = $content;
do something
}
/* And now I want to read file, split the sentence and loop over the array again, but
the last statement will do something else different: */
foreach ($words in $word) {
$content = explode(",", $word); #split a, b, c, d
list($a, $b, $c, $d) = $content;
do something else different
}
我能做些什么来减少这种冗余?如您所见,我无法创建函数,因为最后一条语句对数组做了不同的事情。但是读取文件,拆分句子,分配vars的过程是一样的
谢谢