1
我想從PHP中的文本文檔中讀取問題並將它們排序在array()
中。從純文本文件中讀取問題
結果數組應該是這樣的:
print_r($questionnaire);
array(
'question 1' => array('yes','no'),
'question 2' => array('yes','no'),
'question 3' => array('yes','no'),
...etc
)
我的文本文檔是:
question 1?
yes
no
question 2?
yes
no
question 3?
yes
no
我想這一點:
$txt_doc = $_FILES['txt_doc']['tmp_name'];
$questions_and_answers = array();
$handle = fopen($txt_doc, 'r') or die($txt_doc . ' : CAnt read file');
$i = 0;
while (! feof($handle))
{
$line = trim(fgets($handle));
if(strstr($line, '?'))//its a question
{
$questions_and_answers[$i] = $line;$i++;
}
if(!strstr($line, '?'))
{
$questions_and_answers[$i][] = $line;
}
}
然後當你嘗試時會發生什麼?如果它沒有達到你的期望,可以解釋它在做什麼? –