[英]Create parent child relation JSON tree
这是我的代码,我有三个表帖子表、评论表和喜欢表。 我有一个左连接所有三个表的 postid 在所有三个表中都通用。 我正在完美地获取所有数据,但我正在获取重复的 postdata 数据(例如:Postid=1),我不希望这样。 我的问题是获取 Postdata 的重复数据(例如:Postid=1)
<?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);
?>
我的 output 如下:
[
{
"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
}
}
];
我想要我的 output 如下所示:
{
"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":
'0':{
"Likeid": "1",
"dislikes": "0",
"postid": "1",
"userid": "1"
},
'1':{
"Likeid": "1",
"dislikes": "0",
"postid": "1",
"userid": "1"
},
'2':{
"Likeid": "1",
"dislikes": "0",
"postid": "1",
"userid": "1"
},
'3':{
"Likeid": "1",
"dislikes": "0",
"postid": "1",
"userid": "1"
},
"comments":
'0':{
"commentsid": "1",
"comments": "awesome video",
"Cpostid": "1",
"time": "2020-12-16 17:33:33"
},
'1':{
"commentsid": "1",
"comments": "awesome video",
"Cpostid": "1",
"time": "2020-12-16 17:33:33"
},
'2':{
"commentsid": "1",
"comments": "awesome video",
"Cpostid": "1",
"time": "2020-12-16 17:33:33"
},
'3':{
"commentsid": "1",
"comments": "awesome video",
"Cpostid": "1",
"time": "2020-12-16 17:33:33"
}
},
{
"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
}
}
嗯...起初我认为您的循环是问题所在,而您误解了这种行为。 但是后来我看到了您的“我想要我的 output 如下所示”,这没有任何意义,特别是您想要重复的喜欢/评论。
首先,循环:一个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[] = [
...
];
}
接下来,我们通过使用PostId
索引$menus
来避免重复的帖子。 如果PostId
在$menus
中还不存在,我们插入帖子; 否则我们只是 append 喜欢/评论:
$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()
以使用从零开始的顺序 integer 重新索引它:
foreach (...) { ... }
$menus = array_values($menus);
至于重复的likes
/ comments
,您对此很明确。 但万一你真的想要不重复,我们也可以通过他们的Likeid
和commentsid
索引它们,并根据需要插入/忽略:
$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']);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.