my php code generate a non-Valid json output error
my php code :
$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;
table fields :
Question :
correct :
answer 1 :
answer 2 :
answer 3 :
answer 4 :
Example :
Question : is php a good language ?
correct : 1
answer 1 : yes
answer 2 : no
answer 3 : maybe
answer 4 : good
the output is OK, and formated as I want.
output sample : http://pastebin.com/eefS7KYW
I am sure my php code is correct but I don't know where is exactly the issue !!
============== Fixed : it was just two echo $output !
json_encodeworks fine. The issue must be in your code, but I cannot see what exactly from handpainted screenshots. It would seem you're outputting the result twice.questionsis the only key in the root object it's more likely a double print, otherwise it wouldn't appear right after the 'exception' line.mysql_functions, usemysqliorpdoinstead. You also use variables needlessly.