["postcode"=>"townName"...]
但反而给了我
["postcode=>townName"...]
我的代码:
$sql = "SELECT * FROM uk_postcodes";
$result = mysqli_query($connection, $sql) or die("Error " . mysqli_error($connection));
$dname_list = array();
while($row = mysqli_fetch_array($result))
{
$dname_list[] = $row['postcode']."=>".$row['town'];
}
echo json_encode($dname_list);
答案 0 :(得分:1)
在那一行:
$dname_list[] = $row['postcode']."=>".$row['town'];
您正在使用" =>"创建字符串。在中间(见string concatenation)。您应该将数组的键指定为邮政编码字段,并指定值 - 城镇字段。只需将该行更改为:
$dname_list[$row['postcode']] = $row['town'];