首页 > 解决方案 > 与父级嵌套的 json 数据

问题描述

<?php
    include "../dcon.php";
    $sql = "select * FROM posts left join comments on comments.Cpostid=posts.PostId left join likes on likes.postid=posts.PostId";


        $query=mysqli_query($mysqli,$sql);  
        $result = mysqli_num_rows($query);
        $menus=array();


     for($i=0; $i<$result; $i++){
    $row = mysqli_fetch_all($query,MYSQLI_ASSOC);
     foreach($row as $index => $row ){
        $menus[]=[
        'PostId'=>$row['PostId'],
        'PostVideo'=>$row['PostVideo'],
        'PostDesc'=>$row['PostDesc'],
        'PostImage'=>$row['PostImage'],
        'PostedBy'=>$row['PostedBy'],
        'UserName'=>$row['UserName'],
        'Status'=>$row['Status'],
        'PostParent'=>$row['PostParent'],
        'likes'=>$row['likes'],
        'comments'=>[
        $index => [
        'commentsid'=>$row['commentsId'],
        'comments'=>$row['comments'],
        'Cpostid'=>$row['Cpostid'],
        'time'=>$row['time'],
        ],],
        
        'likes'=>[
        $index =>[
        'Likeid'=>$row['Lid'],
        'dislikes'=>$row['dislikes'],
        'postid'=>$row['postid'],
        'userid'=>$row['userid'],
]]
];
        }
     }  
     echo json_encode($menus,JSON_PRETTY_PRINT);

?>

** 这是我的代码,我有三个表帖子表、评论表和喜欢表。我有一个左连接所有三个表,postid 在所有三个表中都很常见。我完美地获取了所有数据,但我得到了重复的 postdata 数据(例如:Postid=1),我不想要那个。我的问题是获取 Postdata 的重复数据(例如:Postid=1)我想在 Postdata=1 的地方创建 Postdata 的父级,并且我想将所有数据插入父级**

[
        {
            "PostId": "1",
            "PostVideo": "v2.mp4",
            "PostDesc": "this is a tutorial on bench press",
            "PostImage": "null",
            "PostedBy": "Irfan khan",
            "UserName": "irfan",
            "Status": "1",
            "PostParent": null,
            "likes": {
                "Likeid": "1",
                "dislikes": "0",
                "postid": "1",
                "userid": "1"
            },
            "comments": {
                "commentsid": "1",
                "comments": "awesome video",
                "Cpostid": "1",
                "time": "2020-12-16 17:33:33"
            }
        },
        {
            "PostId": "1",
            "PostVideo": "v2.mp4",
            "PostDesc": "this is a tutorial on bench press",
            "PostImage": "null",
            "PostedBy": "Irfan khan",
            "UserName": "khan",
            "Status": "1",
            "PostParent": null,
            "likes": {
                "Likeid": "1",
                "dislikes": "0",
                "postid": "1",
                "userid": "1"
            },
            "comments": {
                "commentsid": "2",
                "comments": "good workouts",
                "Cpostid": "1",
                "time": "2020-12-16 17:33:33"
            }
        },
        {
            "PostId": "1",
            "PostVideo": "v1.mp4",
            "PostDesc": "Chicken generally includes low fat in the meat itself (castrated roosters excluded). The fat is highly concentrated on the skin. A 100g serving of baked chicken breast contains 4 grams of fat and 31 grams of protein, compared to 10 grams of fat and 27 grams of protein for the same portion of broiled, lean skirt steak.",
            "PostImage": "im1",
            "PostedBy": "Rehmat",
            "UserName": null,
            "Status": "1",
            "PostParent": null,
            "likes": {
                "Likeid": "20",
                "dislikes": "0",
                "postid": "1",
                "userid": "4"
            },
            "comments": {
                "commentsid": null,
                "comments": null,
                "Cpostid": null,
                "time": null
            }
        },
        {
            "PostId": "1",
            "PostVideo": "v1.mp4",
            "PostDesc": "Chicken generally includes low fat in the meat itself (castrated roosters excluded). The fat is highly concentrated on the skin. A 100g serving of baked chicken breast contains 4 grams of fat and 31 grams of protein, compared to 10 grams of fat and 27 grams of protein for the same portion of broiled, lean skirt steak.",
            "PostImage": "im1",
            "PostedBy": "Rehmat",
            "UserName": null,
            "Status": "1",
            "PostParent": null,
            "likes": {
                "Likeid": "26",
                "dislikes": "0",
                "postid": "1",
                "userid": "4"
            },
            "comments": {
                "commentsid": null,
                "comments": null,
                "Cpostid": null,
                "time": null
            }
        },
        {
            "PostId": "3",
            "PostVideo": null,
            "PostDesc": "this is an image of chicken",
            "PostImage": "post.jpg",
            "PostedBy": "khan",
            "UserName": null,
            "Status": "0",
            "PostParent": null,
            "likes": {
                "Likeid": null,
                "dislikes": null,
                "postid": null,
                "userid": null
            },
            "comments": {
                "commentsid": null,
                "comments": null,
                "Cpostid": null,
                "time": null
            }
        },
        {
            "PostId": "9",
            "PostVideo": null,
            "PostDesc": "gdhgh",
            "PostImage": "ghghdf",
            "PostedBy": null,
            "UserName": null,
            "Status": null,
            "PostParent": null,
            "likes": {
                "Likeid": null,
                "dislikes": null,
                "postid": null,
                "userid": null
            },
            "comments": {
                "commentsid": null,
                "comments": null,
                "Cpostid": null,
                "time": null
            }
        }
    ];

标签: phpmysqljson

解决方案


推荐阅读