使用两个对象和一个对象字符串在php中创建自己的json输出
我试图获得一个json输出,告诉我用户是否有一个项目(是或否)、用户对该项目的描述,以及每个人对该项目的描述的json数组 我正试图通过以下代码实现这一点:使用两个对象和一个对象字符串在php中创建自己的json输出,php,arrays,json,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
//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);
为什么allNotes:[]中没有任何数据?是否
$numberOfNotes==0
?似乎您的循环没有被执行。没有,我将其回显并得到$numberOfNotes==1writevar\u dump($review)代码>在你的代码中查看修复是什么(如果是$review the pb或不是)。并且如果(numberOfNotes>0)
应该是如果($numberOfNotes>0)
该死的,我是个白痴,完全错过了“$”那就是它!
{"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":[]}