为了账号安全,请及时绑定邮箱和手机立即绑定

数组推送到在php中给出空值的键

数组推送到在php中给出空值的键

PHP
呼唤远方 2022-07-16 18:22:50
所以我从 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();        // =============================================================================
查看完整描述

1 回答

?
GCT1015

TA贡献1827条经验 获得超4个赞

而不是array_push($name['comments'], $res);,使用$name['comments'][] = $res;. 这记录array_push.



查看完整回答
反对 回复 2022-07-16
  • 1 回答
  • 0 关注
  • 210 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号