私は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
}
この冗長性を減らすにはどうすればよいですか?ご覧のように、最後のステートメントは配列とは何か異なるので、関数を作成することはできません。しかし、ファイル、分割文章を読んで、そしてVARSを割り当てるプロセスは同じ
ではなく、「中」の「として」と、私はあなたがforeach($words as $word)
を入力するためのものと仮定していますあなたに