我如何以json格式编码?如果我把第一个放在第一个,第二个和第三个,我收到相同的数据,而如果我把“2nd”=> $ row 1,“3rd”=> $ row 1正在检索与第一个相同的数据。如果我试图输入3它给我一个空。请有人帮忙,谢谢。
这是我的php
$sql = "select
n_name,
shortcut,
case
when rank = 1 then '1st'
when rank = 2 then '2nd'
when rank = 3 then '3rd'
end as rank
from
team inner join nonsport on team.n_id = nonsport.n_id group by n_name order by n_name asc";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
$result = mysqli_query($con,$sql);
$response = array();
while($row=mysqli_fetch_array($result))
{
array_push($response, array("n_name"=>$row[0], "1st"=>$row[1], "2nd"=>$row[2], "3rd"=>$row[2]));
}
echo json_encode (array("nresults"=>$response));
我的预期输出是
实施例。快捷方式有a,b,c,它们的等级a = 1 b = 2 c = 3;
然后1st = a,2nd = b,3rd = c;
我得到的是什么
1st = a,2nd = a,3rd = a
答案 0 :(得分:0)
您的查询为每行返回3个属性“n_name,shortcut,rank”,等级将为“1st”,“2nd”或“3rd”,因此您不会在一行中获得三个等级。我认为你需要在分配之前检查排名的值,如下所示:
while($row=mysqli_fetch_array($result)){
$array = [];
$array["n_name"] = $row[0];
if($row[1]=='a'){
$array["1st"] = $row[1];
} elseif($row[1]=='b'){
$array["2nd"] = $row[1];
} elseif($row[1]=='c'){
$array["3rd"] = $row[1];
}
array_push($response, $array);
}