这是我的代码
$rQuery="SELECT * FROM rowManagr WHERE id=0";
$rResult = $mysqli->query($rQuery)or die($mysqli->error.__LINE__);
while($row = $rResult->fetch_assoc())
{
$rowQuery='SELECT * FROM row_vid WHERE rowid='.$row['rowid'];
$rowResult = $mysqli->query($rowQuery)or die($mysqli->error.__LINE__);
while($rowHome = $rowResult->fetch_assoc())
{
if($rowHome['showid']!=0 )
{
$json[] = array(....);
}
elseif($rowHome['season_id']!=0){
$json[] = array(....);
}
elseif($rowHome['showid']==0 && $rowHome['season_id']==0){
$json[] = array(....);
}
}
$jsondata[$row['rowname']]=$json;
$g['responses']= $jsondata;
}
$jsons = json_encode($g);
echo $jsons;
此代码的输出是(示例)
{"responses":{
"Seasons":[
{"1a"},
{"2a"},
{"3a"},
{"4a"}
],
"Other Show":[
{"1a"},
{"2a"},
{"3a"},
{"4a"},
{"1b"},
{"2b"},
{"3b"},
{"4b"}
],
"Videos":[
{"1a"},
{"2a"},
{"3a"},
{"4a"},
{"1b"},
{"2b"},
{"3b"},
{"4b"},
{"1a"},
{"2a"},
{"3a"},
{"4a"},
{"1c"},
{"2c"},
{"3c"},
{"4c"}
]
} }
季节,其他节目,视频包含4个结果,但在输出季节被复制到其他节目,季节和其他节目被复制到视频。
实际输出应为
{
"responses":{
"Seasons":[
{"1a"},
{"2a"},
{"3a"},
{"4a"}
],
"Other Show":[
{"4a"},
{"1b"},
{"2b"},
{"3b"},
{"4b"}
],
"Videos":[
{"1c"},
{"2c"},
{"3c"},
{"4c"}
]
}
}
由于
答案 0 :(得分:1)
请查看我的代码。
它将为您提供在示例中定义的代码。
这也是一个例子。
<?php
$mysqli = mysqli_connect('localhost','root','','DBName');
$json = '{ responses {"Seasons:["';
$rQuery = "SELECT * FROM TBL_NAME";
$rResult = $mysqli->query($rQuery)or die($mysqli->error.__LINE__);
while($row = $rResult->fetch_assoc())
{
$json .= "{" . $row['name'] . "}" ;
}
$json .= "]}}";
echo $json ;
?>
答案 1 :(得分:1)
只需在内部while循环之前初始化数组$jon
:
while($row = $rResult->fetch_assoc()) {
$rowQuery='SELECT * FROM row_vid WHERE rowid='.$row['rowid'];
$rowResult = $mysqli->query($rowQuery)or die($mysqli->error.__LINE__);
// init $json
$json = array();
while($rowHome = $rowResult->fetch_assoc()) {
if($rowHome['showid']!=0 ) {
$json[] = array(....);
}
...
答案 2 :(得分:0)
使用break;
退出while循环,并使用else if
结构代替nested if
。