I am trying to use PHP to return tName from the database and then output it in the following way: {"tName":["Name1", "Name2"]}) So far it is returning ["Name1", "Name2", "Name3"] but that is not what I want.
Here is my current code
$q = mysql_query("SELECT tName FROM Template");
$result = array();
while($e=mysql_fetch_array($q, MYSQL_ASSOC)){
$result[]=$e['tName'];
}
echo json_encode($result);
1 Answer 1
you just need to add the key.
$result['tName'][]=$e['tName'];
or
json_encode(array("tName" => $result))
Sign up to request clarification or add additional context in comments.
Comments
lang-php
json_encode(array("tName" => $result))?