Please see the link below... http://api.androiddeft.com/cities/cities_array.json
I'm trying to nest my results from the DB into the structure below:
[
{
region:"1",
province:[{
"Ilocos Norte",
"Ilocos Sur"
}]
},
{
region:"2",
province:[{
"Isabela",
"Cagayan"
}]
}
]
However what I have tried isn't working, I'm not able to get the results I am looking for:
$rows = array();
$rs = array();
$result = mysqli_query($connect, "SELECT regDesc, regCode FROM refregion ORDER BY regCode ASC");
while($row=mysqli_fetch_array($result)){
$result2 = mysqli_query($connect, "SELECT provDesc FROM refprovince WHERE regCode = '".$row["regCode"]."' ORDER BY provCode ASC");
while($rs=mysqli_fetch_array($result2))
$rows[] = array(
'region' => $row["regDesc"],
'province'=>$rs["provDesc"]
);
}
echo json_encode($rows);
This instead results in the following output:
[
{
region: "REGION I",
province: "ILOCOS NORTE"
},
{
region: "REGION I",
province: "ILOCOS SUR"
},
{
region: "REGION II",
province: "CAGAYAN"
},
{
region: "REGION II",
province: "ISABELA"
}
]
file_get_contents, thenjson_decodeit (with thetrueflag), then treat it like any normal array, add as many as you wish. you have to do the coding though.