1

我通过 JQuery Ajax 从 PHP 返回一个 JSON 多数组,并且想要确定哪个数组包含一个值以便访问该数组中的值(对于其他数组也是如此 - 最多返回三个数组。

$.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”,值可以是“核”、“放射学”和“超声”。

然后,我需要访问例如 '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 的每个函数来完成此操作。

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 回答