[英]Array push to a key giving null value in php
So I am taking some input from json that is the userid.所以我从作为用户 ID 的 json 获取一些输入。
Then I am trying to find its friends and their blogs and its respective comments.然后我试图找到它的朋友和他们的博客以及他们各自的评论。
Obviously the comments will be more than one.显然评论将不止一个。 So whenever I am doing a array push to those comments to a key in array I get null value in my api response.因此,每当我将数组推送到数组中某个键的那些注释时,我的 api 响应中都会得到空值。
I tried to use simple $name['comments'] = $res.我尝试使用简单的 $name['comments'] = $res。 but as there will be more than one comments so it replaces the new comment with older one.但是因为会有不止一条评论,所以它用旧评论替换了新评论。
please help me solving these problem请帮我解决这些问题
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
}
]
} }
Instead of array_push($name['comments'], $res);
而不是array_push($name['comments'], $res);
, use $name['comments'][] = $res;
, 使用$name['comments'][] = $res;
. . This is documented right at the top of the manual for array_push
.这记录在array_push
手册的顶部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.