使用两个对象和一个对象字符串在php中创建自己的json输出

时间:2013-11-03 03:15:44

标签: php arrays json

我正在尝试使用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:[]中没有任何数据?

0 个答案:

没有答案