搜索

首页  >  问答  >  正文

javascript - 请问php中如何将查询出来的结果数组转化成自己想要的格式,并在前台利用js输出到html中

考试类型的表jx_exam_type,可后台添加内容

考试成绩的表jx_result,可后台添加内容

期中考试成绩表中的exam_id对应考试类型表中的id,也就是添加的成绩是属于期中还是期末

然后使用php查询

$sql="SELECT re.type, re.score, re.exam_id, et.title, DATE_FORMAT(et.addtime, '%Y-%m-%d') AS etime FROM jx_result AS re LEFT JOIN jx_exam_type AS et ON re.exam_id = et.id WHERE re.uid = '$uid' ORDER BY et.addtime DESC";

$result=$db->query($sql);

while($row=$result->fetch_assoc()){
    $arr[]=$row;
}

echo json_encode($arr);

输出的格式如下

[
    {
        "type": "语文",
        "score": "91",
        "exam_id": "2",
        "title": "三年级期末考试",
        "etime": "2017-06-02"
    },
    {
        "type": "英语",
        "score": "89",
        "exam_id": "2",
        "title": "三年级期末考试",
        "etime": "2017-06-02"
    },
    {
        "type": "数学",
        "score": "60",
        "exam_id": "2",
        "title": "三年级期末考试",
        "etime": "2017-06-02"
    },
    {
        "type": "数学",
        "score": "91",
        "exam_id": "1",
        "title": "三年级期中考试",
        "etime": "2017-05-25"
    },
    {
        "type": "语文",
        "score": "85",
        "exam_id": "1",
        "title": "三年级期中考试",
        "etime": "2017-05-25"
    },
    {
        "type": "英语",
        "score": "87",
        "exam_id": "1",
        "title": "三年级期中考试",
        "etime": "2017-05-25"
    }
]

请问我如何才能将以上输出的json格式变为以下这种

{
    "title": "三年级期中考试",
    "etime": "2017-05-25",
    "exam_id": [
        {
            "type": "数学",
            "score": "91",
            "exam_id": "1"
        },
        {
            "type": "语文",
            "score": "85",
            "exam_id": "1"
        },
        {
            "type": "英语",
            "score": "87",
            "exam_id": "1"
        }
    ],
    "title": "三年级期末考试",
    "etime": "2017-06-02",
    "exam_id": [
        {
            "type": "语文",
            "score": "91",
            "exam_id": "2"
        },
        {
            "type": "英语",
            "score": "89",
            "exam_id": "2"
        },
        {
            "type": "数学",
            "score": "60",
            "exam_id": "2"
        }
    ]
}

变为以上这种格式后输出到前台,通过JS来输出到html上面
(可能我写的想要的格式有问题,不过大概意思就是将原来的数据根据exam_id来归类一下再输出)

目前正在学习中,很多地方不是很懂,求指教~~谢谢

某草草某草草2734 天前759

全部回复(3)我来回复

  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-07 09:25:20

    这是我理解的架构,这样前端应该可以遍历到

    while ($row = $result->fetch_assoc()) {
        $scorearr = array("type" => $row["type"], "score" => $row["type"], "exam_id" => $row["exam_id"]);
        if (isset($arr[$row["exam_id"]])) {
            $arr[$row["exam_id"]]["exam_id"][] = $scorearr;
        } else {
            $arr[$row["exam_id"]]["title"] = $row["title"];
            $arr[$row["exam_id"]]["etime"] = $row["etime"];
            $arr[$row["exam_id"]]["exam_id"][0] = $scorearr;
        }
    }
    
    $result = array();
    foreach($arr as $val) {
        $result[] = $val;
    }
    echo $json_encode($result)

    回复
    0
  • 阿神

    阿神2017-06-07 09:25:20

    应该是少包了一层,可以遍历一下你的数组,然后就可以得到你想要的了,然后在json转码就可以了

    回复
    0
  • 滿天的星座

    滿天的星座2017-06-07 09:25:20

    你取数据和处理数据的逻辑有问题,不过你这种的话也可以写

    <?php
    $str = '[
        {
            "type": "语文",
            "score": "91",
            "exam_id": "2",
            "title": "三年级期末考试",
            "etime": "2017-06-02"
        },
        {
            "type": "英语",
            "score": "89",
            "exam_id": "2",
            "title": "三年级期末考试",
            "etime": "2017-06-02"
        },
        {
            "type": "数学",
            "score": "60",
            "exam_id": "2",
            "title": "三年级期末考试",
            "etime": "2017-06-02"
        },
        {
            "type": "数学",
            "score": "91",
            "exam_id": "1",
            "title": "三年级期中考试",
            "etime": "2017-05-25"
        },
        {
            "type": "语文",
            "score": "85",
            "exam_id": "1",
            "title": "三年级期中考试",
            "etime": "2017-05-25"
        },
        {
            "type": "英语",
            "score": "87",
            "exam_id": "1",
            "title": "三年级期中考试",
            "etime": "2017-05-25"
        }
    ]';
    
    $arr = json_decode($str,true);
    
    $result = [];
    $examId = [];
    foreach($arr as $key=>$val){
    
        if(!isset($result[$val['exam_id']])){
            $result[$val['exam_id']]['title'] = $val['title'];
            $result[$val['exam_id']]['etime'] = $val['etime'];
        }
    
        $result[$val['exam_id']]['exam_id'][] = array("type"=>$val['type'],"score"=>$val['score'],"exam_id"=>$val['exam_id']);
    
    }
    
    
    echo json_encode(array_values($result));

    不过你这种取数据的方式不建议,如果数据复杂点 量大点,处理起来就会麻烦

    回复
    0
  • 取消回复