weixin_33705053 2015-02-27 11:16 采纳率: 0%
浏览 3

Ajax成功jQuery PHP

[{ 
    "SchoolId": "015-08-0034-009-37",
    "SubjectId": "08-0034-00613",
    "StudentId": "T-15981",
    "StudentName": "John"
},{
    "SchoolId": "015-08-0034-009-37",
    "SubjectId": "08-0034-00613",
    "StudentId": "T-15982",
    "StudentName": "Paul"
}]

This is the json_encode response from my php page when i do echo if would look like this

Schoolid=015-08-0034-009-37
Subjectid=08-0034-00613
Studentid=T-15981
Studentname=John
Schoolid=015-08-0034-009-37
Subjectid=08-0034-00613
Studentid=T-15982
Studentname=Paul

I can get this result by doing like this

for (var i = 0; i < data.length; i++) {
    console.log(data[i].schoolid);
    console.log(data[i].subjectid);
    console.log(data[i].studentid);
    console.log(data[i].studentname);
}

How can i make it in a way that the value of same entry will be generated only once like the value of school id and subject id will be shown only once the unique value like student id and student name will be shown twice since there are two different values for it.

I am hoping to get the result as

Schoolid=015-08-0034-009-37
Subjectid=08-0034-00613

Studentid=T-15981
Studentname=John
Studentid=T-15982
Studentname=Paul

Where should i do the fixing for this one in php before passing the json_encode or in ajax after receiving the json_encode from php?

if ($stmt - > rowCount() > 0) {
    while ($selected_row = $stmt - > fetch(PDO::FETCH_ASSOC)) {
        $basicinfo[] = array('schoolid' => $selected_row['schoolid'], 'subjectid' => $selected_row['subjectid'], 'studentid' => $selected_row['studentid'], 'studentname' => $selected_row['studentname']);
        //$basicinfo1[] = array('schoolid' => $selected_row['schoolid'], 'subjectid' => $selected_row['subjectid']);
        //$basicinfo2[] = array('studentid' => $selected_row['studentid'], 'studentname' => $selected_row['studentname']);
    }
    //$merge = array_merge($basicinfo1 , $basicinfo2);
    //$add = $basicinfo1 + $basicinfo2)
    //$input = array_map("unserialize", array_unique(array_map("serialize", $merge)));
    $input = array_map("unserialize", array_unique(array_map("serialize", $basicinfo)));
    echo json_encode($input, JSON_UNESCAPED_UNICODE);
}
  • 写回答

1条回答 默认 最新

  • weixin_33716941 2015-02-27 13:37
    关注

    Following code will help you fix the array in PHP

    if ($stmt - > rowCount() > 0) {
        //$new_data = array();
        while ($v = $stmt - > fetch(PDO::FETCH_ASSOC)) {
            $tmp_key = $v['SchoolId'].'-'.$v['SubjectId']; //adding a temp key
    
            $new_data[$tmp_key]['SchoolId'] = $v['SchoolId'];
            $new_data[$tmp_key]['SubjectId'] = $v['SubjectId'];
    
            $sudky = (isset($new_data[$tmp_key]['Student'])) ? count($new_data[$tmp_key]['Student']) : 0; //getting the key for student child array
            $new_data[$tmp_key]['Student'][$sudky]['StudentId'] = $v['StudentId'];
            $new_data[$tmp_key]['Student'][$sudky]['StudentName'] = $v['StudentName'];
        }
        $new_data = array_values($new_data);
        echo json_encode($new_data, JSON_UNESCAPED_UNICODE);
    }
    

    The final JSON data will be like following

    [{
        "SchoolId":"015-08-0034-009-37",
        "SubjectId":"08-0034-00613",
        "Student":[
            {"StudentId":"T-15981","StudentName":"John"},
            {"StudentId":"T-15982","StudentName":"Paul"}
        ]
    }]
    

    In Javascript you will have to add another loop inside the for loop you alreardy have that goes through Student data

    Following is the Javascript section

    <script>
    $(document).ready(function($){
        $.ajax({
            type: "post",
            url: "YOURSCRIPT",
            dataType: "json",
            success: function(data){
                if(data!=''){
                    for (var i = 0; i < data.length; i++) {
                        alert(data[i].SchoolId);
                        alert(data[i].SubjectId);
                        var student = data[i].Student;
                        for (var j = 0; j < student.length; j++) {
                            alert(student[j].StudentId);
                            alert(student[j].StudentName);
                        }
                    }
                }
            }
        });
    });
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?