2

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

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'];

http://php.net/manual/en/language.types.array.php

answered Nov 30, 2015 at 15:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.