[数组推入键,在php中给出空值

问题描述 投票:-1回答:1

因此,我从json接收一些输入,即userid。

然后,我试图找到它的朋友,他们的博客以及它们各自的评论。

显然,评论将不止一个。因此,每当我执行数组操作时,将这些注释推入数组中的键时,我的api响应中都会得到null值。

我尝试使用简单的$ 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
        }
]

}

php arrays mysqli array-push
1个回答
0
投票

代替array_push($name['comments'], $res);,使用$name['comments'][] = $res;。在manual for array_push的顶部记录了该信息。

© www.soinside.com 2019 - 2024. All rights reserved.