嗨我是新的PHP和我不知道我该怎么做?如何在PHP中对Json数组进行编码?
我在我的数据库中有四行,我想用php编码这些行的Json数组我怎么能这样做。
下面是我的代码,请考虑这一点,并给我一个suggestion--
<html>
<head>
<title>First</title>
<body>
<?php
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ContactDB", $con);
$result = mysql_query("SELECT * FROM mycontacts");
echo "<table border='1'>
<tr>
<th>PhoneNumber</th>
<th>Name</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
$data=array("PhoneNumber"=>$row['PhoneNumber'],"Name"=>$row['Name']);
print_r (json_encode(array_chunk($data, 1, true)));
}
mysql_close($con);
?>
</body>
</head>
</html>
我得到响应这样
[{ “******中国”: “1234567”},{“名称“:”Ujjwal“}] [{”PhoneNumber“:”765423“},{”Name“:”ABC“}] [{”PhoneNumber“:”098765123“},{”Name“:”A“}]
但我想要这种格式
[{“PhoneNumber”:“1234567”,“Name”:“X”}, { “******中国”: “765423”, “姓名”: “ABC”},{ “******中国”: “098765123”, “姓名”: “A”}]
的可能重复[执行opendir功能给了我多个阵列,而不是仅仅一个(http://stackoverflow.com/questions/9341025/opendir-function-gives-me-multiple-arrays-instead-of-只是一个) – mario 2012-03-06 13:33:09