$value) { $answer = new Answer(); $answer->id = trim($key); $answer->text = trim($value); if($key == $r['correct']){$answer->correct = true;} $answers[] = $answer; } $data[] = array( "id" => trim($r['id']), "question" => trim($r['question']), "answers" => $answers ); } header('Content-type: application/json'); echo json_encode( $data ); ?>