I'm building a simple service for mobile apps with php which give a JSON to mobile apps. I have two table in code, below the snippet :
$con = mysqli_connect(HOST, USER, PASS, DB);
$sql_results = mysqli_query($con, "SELECT * FROM `table-images`");
$sql_results_1 = mysqli_query($con, "SELECT * FROM `table-code` WHERE id='$id'");
$rows = array();
while($r = mysqli_fetch_assoc($sql_results)) {
$rows[] = $r;
}
echo'{"response":'.json_encode($rows).'}';
Resul code above :
{
"response": [{
"id": "31",
"shirtImage": "Content\/Images\/Short Sleeve\/874be7b82812f76c944d71706c9651eb.gif"
}, {
"id": "32",
"shirtImage": "Content\/Images\/Short Sleeve\/b-Cleaned.png"
}]
}
I want to put result of sql_results_1 as key to the result, here I the JSON I need :
{
"response": [{
"id": "31",
"shirtImage": "Content\/Images\/Short Sleeve\/874be7b82812f76c944d71706c9651eb.gif",
"idcode": [{
"X": 0,
"Y": 1,
"Z": 2
}]
}, {
"id": "32",
"shirtImage": "Content\/Images\/Short Sleeve\/b-Cleaned.png",
"idcode": [{
"X": 2,
"Y": 1,
"Z": 0
}]
}]
}
Where key idcode is result from '$sql_results_1'. I new in PHP programming, I read some question regarding my own but the result is not I expect.
Edit
As @Ashu answer, here the code now :
$final_array = array();
$i=0;
while($r = mysqli_fetch_assoc($sql_results)) {
array_push($final_array,$r);
$res = mysqli_query($con, "SELECT * FROM `table-code` WHERE id='".$row['id']."'");
while($r = mysqli_fetch_assoc($res)) {
$final_array[$i]["idcode"] = array("x"=>$r['x']);
}
$i++;
}
But the key idcode is JSONObject "idcode":{x=0}, I need it as JSONArray "idcode":[{x=0}]
$sql_results_1? what did you try by far ?table-code