数组推送到 php 中给出空值的键
Array push to a key giving null value in php
所以我从 json 获取一些输入,即用户 ID。
然后我试图找到它的朋友和他们的博客及其各自的评论。
显然评论会不止一个。因此,每当我将这些评论推送到数组中的键时,我都会在 api 响应中得到空值。
我尝试使用简单的 $name['comments'] = $res。但由于会有多个评论,所以它用旧评论替换新评论。
请帮我解决这些问题
CoDE->
$userdata = json_decode(file_get_contents('php://input'));
$userid = mysqli_real_escape_string($conn, $userdata->userid);
// =============================================================================
// ===========================Get Friends=======================================
// =============================================================================
$friends = " SELECT `friend_one`
FROM `friends`
WHERE `friend_two` = '$userid' AND `status` = '1'
UNION ALL
SELECT `friend_two`
FROM `friends`
WHERE `friend_one` = '$userid' AND `status` = '1'
";
$response = array();
$friendsq = mysqli_query($conn, $friends);
if(!$friendsq) {
$response['statusCode'] = 400;
$response['message'] = "failed to connect to backend. please contact developer";
}
$count = mysqli_num_rows($friendsq);
// =============================================================================
// ===========================No Friends=======================================
// =============================================================================
if($count == 0) {
$response['statusCode'] = 202;
$response['message'] = "No Friends to show the newsfeed";
} else {
$response['statusCode'] = 200;
$response['message'] = "Success";
$response['microblogs'] = array();
$name['comments'] = array();
// =============================================================================
// ===========================show blogs of Friends=============================
// =============================================================================
while($friendlist = mysqli_fetch_assoc($friendsq)) {
$friends = $friendlist['friend_one'];
$getblog = "SELECT DISTINCT t1.*,t2.username,t2.profile_pic1 FROM microblogs AS t1 INNER JOIN users AS t2 ON t1.user_id = t2.user_id WHERE (t2.user_id = '$friends') ORDER BY t1.id DESC";
// $getblog = "SELECT * FROM `microblogs` WHERE `user_id` = '$friends' OR `user_id` = '$userid' ORDER BY `id` DESC";
$getblogq = mysqli_query($conn, $getblog);
while($name = mysqli_fetch_assoc($getblogq)) {
$blgid = $name['id'];
// =============================================================================
// ===========================show comments=============================
// =============================================================================
$getcomment = "SELECT t1.*, t2.username, t2.profile_pic1 FROM `comments` AS t1 INNER JOIN `users` AS t2 ON t1.user_id = t2.user_id INNER JOIN `microblogs` AS t3 ON t3.id = t1.microblog_id WHERE t3.id ='$blgid'";
$getcommentq = mysqli_query($db, $getcomment);
if($getcommentq) {
$count = mysqli_num_rows($getcommentq);
if($count >= 1){
while($res = mysqli_fetch_assoc($getcommentq)) {
// echo "found";
array_push($name['comments'], $res); //this gives null
}
} else {
// echo "not found";
array_push($name['comments'], "");
}
}
http_response_code(200);
array_push($response['microblogs'], $name);
}
}
}
response->
{
"statusCode": 200,
"message": "Success",
"microblogs": [
{
"id": "23",
"user_id": "40",
"blogname": "djd",
"location": "bhubaneswar",
"image": "https://www.gvitechnology.com/gypsi/api/uploads/blogimg/722e99a7b1a21a0074ac6015912c25ed.jpeg",
"data": "jjncdn",
"Likes": "0",
"username": "satya",
"profile_pic1": "https://www.gvitechnology.com/gypsi/api/uploads/2d39859f7165ebc57bf02e613f435395.jpeg",
"comments": null
},
{
"id": "7",
"user_id": "12",
"blogname": "test",
"location": "cuttack",
"image": "https://www.gvitechnology.com/gypsi/api/uploads/blogimg1.jpg",
"data": "This is a nice place.i like it here.sgdyusdhsj",
"Likes": "4",
"username": "subha",
"profile_pic1": "5e102c72eb6d2.",
"comments": null
}
]
}
而不是 array_push($name['comments'], $res);
,使用 $name['comments'][] = $res;
。这记录在 manual for array_push
.
的顶部
所以我从 json 获取一些输入,即用户 ID。
然后我试图找到它的朋友和他们的博客及其各自的评论。
显然评论会不止一个。因此,每当我将这些评论推送到数组中的键时,我都会在 api 响应中得到空值。
我尝试使用简单的 $name['comments'] = $res。但由于会有多个评论,所以它用旧评论替换新评论。
请帮我解决这些问题
CoDE->
$userdata = json_decode(file_get_contents('php://input'));
$userid = mysqli_real_escape_string($conn, $userdata->userid);
// =============================================================================
// ===========================Get Friends=======================================
// =============================================================================
$friends = " SELECT `friend_one`
FROM `friends`
WHERE `friend_two` = '$userid' AND `status` = '1'
UNION ALL
SELECT `friend_two`
FROM `friends`
WHERE `friend_one` = '$userid' AND `status` = '1'
";
$response = array();
$friendsq = mysqli_query($conn, $friends);
if(!$friendsq) {
$response['statusCode'] = 400;
$response['message'] = "failed to connect to backend. please contact developer";
}
$count = mysqli_num_rows($friendsq);
// =============================================================================
// ===========================No Friends=======================================
// =============================================================================
if($count == 0) {
$response['statusCode'] = 202;
$response['message'] = "No Friends to show the newsfeed";
} else {
$response['statusCode'] = 200;
$response['message'] = "Success";
$response['microblogs'] = array();
$name['comments'] = array();
// =============================================================================
// ===========================show blogs of Friends=============================
// =============================================================================
while($friendlist = mysqli_fetch_assoc($friendsq)) {
$friends = $friendlist['friend_one'];
$getblog = "SELECT DISTINCT t1.*,t2.username,t2.profile_pic1 FROM microblogs AS t1 INNER JOIN users AS t2 ON t1.user_id = t2.user_id WHERE (t2.user_id = '$friends') ORDER BY t1.id DESC";
// $getblog = "SELECT * FROM `microblogs` WHERE `user_id` = '$friends' OR `user_id` = '$userid' ORDER BY `id` DESC";
$getblogq = mysqli_query($conn, $getblog);
while($name = mysqli_fetch_assoc($getblogq)) {
$blgid = $name['id'];
// =============================================================================
// ===========================show comments=============================
// =============================================================================
$getcomment = "SELECT t1.*, t2.username, t2.profile_pic1 FROM `comments` AS t1 INNER JOIN `users` AS t2 ON t1.user_id = t2.user_id INNER JOIN `microblogs` AS t3 ON t3.id = t1.microblog_id WHERE t3.id ='$blgid'";
$getcommentq = mysqli_query($db, $getcomment);
if($getcommentq) {
$count = mysqli_num_rows($getcommentq);
if($count >= 1){
while($res = mysqli_fetch_assoc($getcommentq)) {
// echo "found";
array_push($name['comments'], $res); //this gives null
}
} else {
// echo "not found";
array_push($name['comments'], "");
}
}
http_response_code(200);
array_push($response['microblogs'], $name);
}
}
}
response->
{
"statusCode": 200,
"message": "Success",
"microblogs": [
{
"id": "23",
"user_id": "40",
"blogname": "djd",
"location": "bhubaneswar",
"image": "https://www.gvitechnology.com/gypsi/api/uploads/blogimg/722e99a7b1a21a0074ac6015912c25ed.jpeg",
"data": "jjncdn",
"Likes": "0",
"username": "satya",
"profile_pic1": "https://www.gvitechnology.com/gypsi/api/uploads/2d39859f7165ebc57bf02e613f435395.jpeg",
"comments": null
},
{
"id": "7",
"user_id": "12",
"blogname": "test",
"location": "cuttack",
"image": "https://www.gvitechnology.com/gypsi/api/uploads/blogimg1.jpg",
"data": "This is a nice place.i like it here.sgdyusdhsj",
"Likes": "4",
"username": "subha",
"profile_pic1": "5e102c72eb6d2.",
"comments": null
}
]
}
而不是 array_push($name['comments'], $res);
,使用 $name['comments'][] = $res;
。这记录在 manual for array_push
.