我的PHP代碼生成非有效的JSON輸出誤差輸出誤差
我的PHP代碼:
$questions = array();
while($question = mysql_fetch_array($result, MYSQL_ASSOC)) {
$questions[] = array('question'=> $question);
}
print_r ($questions);
$newQuestions = array('questions' => array());
foreach($questions as $key => $question){
$newQuestion = array(
'question' => $question['question']['question'],
'correct' => $question['question']['correct'],
'answers' => array(
$question['question']['answer1'],
$question['question']['answer2'],
$question['question']['answer3'],
$question['question']['answer4']
)
);
$newQuestions['questions'][] = $newQuestion;
}
$output = json_encode(($newQuestions),JSON_UNESCAPED_UNICODE);
echo '<br/><br/>';
echo $output;
表字段:
Question :
correct :
answer 1 :
answer 2 :
answer 3 :
answer 4 :
實施例:
Question : is php a good language ?
correct : 1
answer 1 : yes
answer 2 : no
answer 3 : maybe
answer 4 : good
輸出正常,並格式化爲a我想要。
輸出樣本:http://pastebin.com/eefS7KYW
我相信我的PHP代碼是正確的,但我不知道哪裏是完全的問題!
============== 修正:它只是兩個echo $輸出!
'。該問題必須在您的代碼中,但我無法看到手繪屏幕截圖中的具體內容。這似乎是你輸出結果兩次。 – 2014-09-04 23:17:12
看起來像缺少一個逗號 – RicardoE 2014-09-04 23:20:21
@RicardoE作爲'問題'是根對象中唯一的關鍵,它更可能是雙重打印,否則它不會出現在'異常'行之後。 – 2014-09-04 23:27:19