0

我试图有一个 json 输出,它告诉我用户是否有一个项目(是或否),来自用户的项目描述,以及第三个来自每个人的描述的 json 数组。

我正在尝试使用以下代码来完成此操作:

//prepare variables for getting other user notes
$review = array();

if (numberOfNotes > 0) {
    for ($i=0; $i < $numberOfNotes ; $i++) {
        //get row
        $row = $resultNotes->fetch_assoc();
        $writerID = $row['userID'];
        $tastedBeerID = $row['beerID'];
        $noteID = $row['noteID'];
        $note = $row['note'];

        $note = urldecode($note);

        //get username from id
        //get user name
        @ $db = new myConnectDB();
        $query = "SELECT userName FROM user WHERE userID = $writerID";
        $result2 = $db->query($query);
        $row2 = $result2->fetch_assoc();
        $writerName = $row2['userName'];

        //trim note to 200 characters
        $note = limitwords($note,150,$trail='...');

        $count = $i + 1;

        //add to array of notes
        $review[] = array('userName' => "$writerName" ,'review' => "$note"   );
    }
}


//print yes if user has beer
$d = array('status' => 'yes' ,'userNote' => $alreadyWrittenNote , 'allNotes' => $review  );
}

$jsonCode = json_encode($d);

我的 $d 中有前两个项目可以正常工作,但我无法让“allNotes”输出一个 $review[] 数组。

我得到这个作为我的输出:

{"status":"yes","userNote":"  Pours a dark brown with a two finger head, with initial smell of a really smooth bourbon. \n\nFirst impression, a really smooth bourbon with roasted hints. While drinking this I can not express how smooth it is, but it ends with a nice slight tingling sensation on the tongue which really rounds the beer off nicely. There are also very slight hints of chocolate and a medium bitterness. ","allNotes":[]}

为什么 allNotes:[] 里面没有任何数据?

4

0 回答 0