[英]Creating JSON array with information retrieved from database
I am trying to create a JSON array with the information selected from a database but I cannot give the array a name. 我正在尝试使用从数据库中选择的信息创建一个JSON数组,但无法给该数组起一个名字。
while($row = mysql_fetch_array($result))
{
$arr = array('isim' => $row['ename'], 'yer' => $row['eplace'], 'top' => $row['society'], 'tar' => $row['edate'], 'saat' => $row['ehour']);
echo json_encode($arr);
}
I want to see the result; 我想看看结果;
{"events":[{"isim":"eere","yer":"dddd","top":"asdfsdffgdfgdfg","tar":"2013-10-18","saat":"12:46"}{"isim":"fhjfr","yer":"yhjrhj","top":"ryjryjrj","tar":"2013-10-30","saat":"12:45"}{"isim":"sfsgsg","yer":"sfgssfg","top":"sgsfgsg","tar":"2013-10-31","saat":"12:45"}]} {“ events”:[{“ isim”:“ eere”,“ yer”:“ dddd”,“ top”:“ asdfsdffgdfgdfg”,“ tar”:“ 2013-10-18”,“ saat”:“ 12: 46“} {” isim“:” fhjfr“,” yer“:” yhjrhj“,” top“:” ryjryjrj“,” tar“:” 2013-10-30“,” saat“:” 12:45“} {“ isim”:“ sfsgsg”,“ yer”:“ sfgssfg”,“ top”:“ sgsfgsg”,“ tar”:“ 2013-10-31”,“ saat”:“ 12:45”}]}
But I cannot see the 但是我看不到
{"events":[ {“事件”:[
in the beggining and 在开始和
]} ]}
at the end. 在末尾。
Thank you. 谢谢。
To generate valid JSON, you first need to add everything to a multi-dimensional array and only then when that is complete, encode it: 要生成有效的JSON,首先需要将所有内容添加到多维数组,然后在完成后对其进行编码:
$arr = array();
while($row = mysql_fetch_array($result))
{
$arr[] = array('isim' => $row['ename'], 'yer' => $row['eplace'], 'top' => $row['society'], 'tar' => $row['edate'], 'saat' => $row['ehour']);
// or perhaps just: $arr[] = $row;
}
echo json_encode($arr);
Also note that the mysql_*
functions are deprecated. 另请注意,不建议使用mysql_*
函数。
To put everything under an events
key, you would need something like: 要将所有内容置于events
键下,您将需要以下内容:
$arr['events'][] = array('isim' => $row['ename'], 'yer' => $row['eplace'], 'top' => $row['society'], 'tar' => $row['edate'], 'saat' => $row['ehour']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.