创建父子关系 JSON 树

时间:2020-12-21 16:46:07

标签: php

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

{1: [0, 1, 4], 3: [2], 4: [5, 6], 5: [3]}

我的输出如下:

<?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": "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
        }
    }
];

1 个答案:

答案 0 :(得分:0)

嗯...起初我以为是你的循环有问题,而你误解了行为。但后来我看到你的“我想要我的输出如下”,这没有意义,特别是你想要重复的喜欢/评论。

首先,循环:for 循环和内部 foreach 循环。 for 循环遍历结果数。内部 foreach 循环遍历所有结果。所以如果我们有 6 个结果,不是只迭代 6 次,而是迭代 36 次(6 个结果计数 x 6 个结果)。

我们实际上只需要 1 个循环:

$query = mysqli_query($mysqli, $sql);
$menus = [];
$rows = mysqli_fetch_all($query, MYSQLI_ASSOC);
foreach ($rows as $index => $row) {
    $menus[] = [
        ...
    ];
}

接下来,我们通过使用 $menus 索引 PostId 来避免重复帖子。如果 PostId 中尚不存在 $menus,我们插入帖子;否则,我们只需添加点赞/评论:

$query = mysqli_query($mysqli, $sql);
$menus = [];
$rows = mysqli_fetch_all($query, MYSQLI_ASSOC);
foreach ($rows as $row) {
    if (!isset($menus[$row['PostId']])) {
        $menus[$row['PostId']] = [
            'PostId' => $row['PostId'],
            ...
            'comments' => [],
            'likes' => [],
        ];
    }

    $menus[$row['PostId']]['comments'][] = [
        ...
    ];
    $menus[$row['PostId']]['likes'][] = [
        ...
    ];
}

如果由于某种原因您不希望 $menus 索引 PostId,我们可以在循环后调用 array_values() 以使用从零开始的顺序整数重新索引它:

foreach (...) { ... }
$menus = array_values($menus);

至于重复的 likes/comments,您已经明确说明了。但以防万一你真的想要非重复,我们也可以通过它们的 Likeidcommentsid 索引它们,并根据需要插入/忽略:

$query = mysqli_query($mysqli, $sql);
$menus = [];
$rows = mysqli_fetch_all($query, MYSQLI_ASSOC);
foreach ($rows as $row) {
    if (!isset($menus[$row['PostId']])) {
        $menus[$row['PostId']] = [
            'PostId' => $row['PostId'],
            ...
            'comments' => [],
            'likes' => [],
        ];
    }

    if (!isset($menus[$row['PostId']]['comments'][$row['commentsid']])) {
        $menus[$row['PostId']]['comments'][$row['commentsid']] = [
            ...
        ];
    }

    if (!isset($menus[$row['PostId']]['Likes'][$row['Lid']])) {
        $menus[$row['PostId']]['Likes'][$row['Lid']] = [
            ...
        ];
    }
}

如果我们也不希望 comments/likes 索引 commentsid/Lid,我们也可以对它们调用 array_values()

foreach (...) { ... }
$menus = array_values($menus);
foreach ($menus as &$menu) {
    $menu['comments'] = array_values($menu['comments']);
    $menu['likes'] = array_values($menu['likes']);
}
unset($menu);
// Or
array_walk($menus, function (&$menu) {
    $menu['comments'] = array_values($menu['comments']);
    $menu['likes'] = array_values($menu['likes']);
});
相关问题