I have a webservice which gives data as json form mysql database.
PHP webservice
<?php
$conn = mysql_connect('localhost','root','');
mysql_select_db('db', $conn);
$query = mysql_query("SELECT id,group FROM faq");
//$query content is:
// Array ([id]=>faq1 [group]=>hardware) Array ([id]=>faq2 [group]=>software)
$faq = array();
while($row = mysql_fetch_assoc($query)) {
$faq[] = $row // <= ????????
}
header('Content-type: application/json');
return json_encode($faq);
?>
JSON output that I get
[
{
"id": "faq1",
"group": "hardware"
},
{
"id": "faq2",
"group": "software"
}
]
JSON output that I want to have
[
{
"id": "faq1",
"group": {
"id": "hardware"
}
},
{
"id": "faq2",
"group": {
"id": "software"
}
}
]
I want to get this JSON data as result but I really couldn't have success on this. So how should I need to edit my "PHP webservice" to get this json result?
$faq = array();
while($row = mysql_fetch_assoc($query)) {
$faq[] = array(
'id' => $row['id'],
'group' => array(
'id' => $row['group']
)
);
}
Do this first:
$row['group'] = Array('id' => $row['group']);
Construct array like this
$arr = array();
$id = 0;
while($row = mysql_fetch_assoc($query)) {
$arr[$i]['id'] = $row['id'];
$arr[$i]['group']['id'] = $row['group'];
$i++;
}
$json = json_encode($arr);
try this :
$arrayJson["id"] = $row["id"];
$arrayJson["group"]["id"] = $row["group"];
echo json_encode($arrayJson)