我正在学习如何使用PHP。我将文件内容读入数组,并为数组中的每个索引分配变量名称。PHP foreach循环冗余
例如:
$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
}
我能做些什么来减少这种冗余?正如你所看到的,我不能做一个函数,因为最后一条语句做了与数组不同的事情。但是,读文件,拆分句子,并指派瓦尔过程是相同的
谢谢