1

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"}]
4

2 に答える 2

3

これは、jQuery のeach関数で実現できます。

function(returnedData) {
  $.each(returnedData, function(i,o) {
      if (o.image_report_type == "nuclear") {
         //Do Something
      }
  });
}

これがjsFiddleで、このテクニックを示しています

于 2013-09-05T03:23:15.417 に答える
0

これを試してくださいhttp://jsfiddle.net/mN4LF/3/

Javascript

function findIndexOf(data, searchField, searchValue){
    var result = new Array();
    for(var i = 0; i < data.length; i++){
        if(data[i][searchField] == searchValue){
            result.push(i);
        }
    }

    return result;
}
于 2013-09-05T03:28:23.650 に答える