javascript - 請問php中如何將查詢出來的結果陣列轉換成自己想要的格式,並在前台利用js輸出到html中
某草草
某草草 2017-06-07 09:23:20
0
3
735

考試類型的表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來歸類一下再輸出)

目前正在學習中,很多地方不是很懂,求指教~~謝謝

某草草
某草草

全部回覆(3)
女神的闺蜜爱上我

這是我理解的架構,這樣前端應該可以遍歷到

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)
阿神

應該是少包了一層,可以遍歷一下你的數組,然後就可以得到你想要的了,然後在json轉碼就可以了

滿天的星座

你取資料和處理資料的邏輯有問題,不過你這種的話也可以寫

<?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));

不過你這種取資料的方式不建議,如果資料複雜點 量大點,處理起來就會麻煩

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!