user3594787
user3594787

Reputation: 1

Encoding a JSON Array using PHP

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);

Upvotes: 0

Views: 33

Answers (1)

Yamiko
Yamiko

Reputation: 5453

you just need to add the key.

$result['tName'][]=$e['tName'];

or

json_encode(array("tName" => $result))

Upvotes: 1

Related Questions