Я изучаю, как использовать PHP. Я прочитал содержимое файла в массиве и назначил имя переменной для каждого индекса в массиве.php foreach loop redundancy
Например:
$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
}
Что я могу сделать, чтобы уменьшить эту избыточность? Как вы можете видеть, я не могу создать функцию, потому что последний оператор делает что-то другое для массива. Но процесс чтения файла, разделив предложения, и назначение ВАР те же
Спасибо