im retrieving some data in my database table and than convert to json, but i need to create a array with this structire in my php loop
["postcode"=>"townName"...]
but instead is giving me
["postcode=>townName"...]
My code:
$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);
asked Nov 30, 2015 at 15:26
1 Answer 1
In that line:
$dname_list[] = $row['postcode']."=>".$row['town'];
You're creating a string with "=>" in the middle (see string concatenation). You should specify the key of the array to be the postcode field, and the value - town field. Just change that line to:
$dname_list[$row['postcode']] = $row['town'];
answered Nov 30, 2015 at 15:32
Comments
lang-php