嗨,大家需要一些帮助。
所以我试图像这样回应一个json数组格式:
{"ttype": [{ "id":"1", "type":"Order No.","code":"ORDNO" },
{"id":"2", "type":"Orderline", "code":"ORDLINE"} ]
,"input":[{"id":"1","order":"Order No.", "limit":"5"},
{"id":"2","order":"Order No.", "limit":"5"},
{"id":"2", "order":"Order Line No.","limit":"10"}]}
所以这是我的代码:
$query = "SELECT * FROM `order` WHERE is_deleted = 0 ORDER BY id ASC";
$que = mysql_query($query);
while($row = mysql_fetch_array($que)){
$id = $row['id'];
$keyword = $row['keyword'];
$parameters = $row['parameters'];
$json = array(
'ttype'=>array(array(
'id'=>$id,
'type'=>'',
'code'=>$keyword
)),'input'=>array(array(
'id' =>$id,
'order'=>'',
'limit'=>20
))
);
echo json_encode($json);
}
我遇到的问题是:
{"ttype":[{"id":"1","type":"","code":"ORDNO"}],"input":[{"id":"1","order":"","limit":20}]}{"ttype": [{"id":"2","type":"","code":"ORDLINE"}],"input": [{"id":"2","order":"","limit":20}]}
知道如何正确地给出正确的格式吗? 感谢
答案 0 :(得分:0)
你必须在while循环中有两个变量,然后相应地使用你的结构变量json_encode。这是您完成工作的方式
答案 1 :(得分:0)
我已经解决了我的问题。
所以这是我的代码:
$query = "SELECT * FROM `order` WHERE is_deleted = 0 ORDER BY id ASC";
$que = mysql_query($query);
while($row = mysql_fetch_array($que)){
$id = $row['id'];
$keyword = $row['keyword'];
$parameters = $row['parameters'];
$array1[] = array(
'id'=>$id,
'type'=>'',
'code'=>$keyword
);
$array2[] = array(
'id'=>$id,
'order'=>'',
'limit'=>20
);
}
$globalarray = array(
'ttype'=>$array1,
'input'=>$array2
);
echo json_encode($globalarray);
我现在得到了这个结果:
{"ttype":[{"id":"1","type":"","code":"ORDNO"}, {"id":"2","type":"","code":"ORDLINE"}],"input":[{"id":"1","order":"","limit":20},{"id":"2","order":"","limit":20}]}
感谢您的帮助! 特别是对Waleed Ahmed先生来说。 我欠你的,你的答案给了我一个让这成为可能的想法。
非常感谢!
答案 2 :(得分:0)
尝试以下代码。
$query = "SELECT * FROM `order` WHERE is_deleted = 0 ORDER BY id ASC";
$que = mysql_query($query);
while($row = mysql_fetch_array($que)){
$id = $row['id'];
$keyword = $row['keyword'];
$parameters = $row['parameters'];
$json[] = array(
'ttype'=>array(array(
'id'=>$id,
'type'=>'',
'code'=>$keyword
)),'input'=>array(array(
'id' =>$id,
'order'=>'',
'limit'=>20
))
);
}
echo json_encode($json);