JQuery Ajax を介して PHP から JSON マルチ配列を返しています。その配列の値にアクセスするために、どの配列に値が含まれているかを判断したいと考えています (他の配列についても同様に、合計で最大 3 つの配列が返されます。
$.ajax({
type: "POST",
url: "scripts/get_model_imaging_report.php",
data: {
case_id:caseId,
level:currentLevel
},
dataType: "json",
success: function(returnedData) {
}
});
PHP;
$case = $_POST['case_id'];
$level = $_POST['level'];
$query = "SELECT * FROM model_image_report WHERE case_fk = '$case' AND level = '$level'";
$result = mysql_query($query, $connection) or die(mysql_error());
$data = array();
while ($row = mysql_fetch_array($result)) {
$data[] = array(
'image_report_type' => $row['image_report_type'],
'subject' => $row['subject'],
'clinical' => $row['clinical'],
'study_type' => $row['study_type'],
'report' => $row['report'],
'comment' => $row['comment'],
'image' => $row['image'],
'image_annotated' => $row['image_annotated']
);
}
echo json_encode($data);
確認する必要があるキーは「image_report_type」で、値は「nuclear」、「radiology」、および「ultrasound」です。
次に、たとえば 'image_report_type' == 'nuclear' のような配列の値にアクセスする必要があります。
JSON 配列は次のようになります。
[{"image_report_type":"nuclear","subject":"Brain Scan","clinical":"Clinical","study_type":"nuclear image scan.","report":"Report","comment":"Comment","image":"original_image_case_3_level_1_nuclear.jpg","image_annotated":"annotated_image_case_3_level_1_nuclear.png"},{"image_report_type":"ultrasound","subject":"Brain Scan","clinical":"Clinical","study_type":"Ultrasound image scan.","report":"Report","comment":"Comment","image":"original_image_case_3_level_1_nuclear.jpg","image_annotated":"annotated_image_case_3_level_1_nuclear.png"}]