orange orange - 17 days ago 5
JSON Question

2 select query in different array on one json object

How do i encode two arrays that retrieves data from 2 different table in my database and encode it in 1 json response

Here is my php
$sql = "select * from schedule;";
$sql1 = "select * from matches;";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);

$result = mysqli_query($con,$sql);
$result1 = mysqli_query($con,$sql1);


$response = array();
while($row=mysqli_fetch_array($result))
{
array_push($response, array("n_name"=>$row[1],"start"=>$row[4],"end"=>$row[5],"venue"=>$row[6]));

}

$data= array();
while($row=mysqli_fetch_array($result1))
{
array_push($data, array("teamone"=>$row[1], "teamtwo"=>$row[2], "s_name"=>$row[10]));
}



echo json_encode (array("server_response"=>$response, $data));





mysqli_close($con);
?>


What i want is something like this

{
"server_response": [{
"n_name": null,
"start": "2016-11-09 00:00:00",
"end": "2016-11-16 00:00:00",
"venue": "aaaaaa",
"teamone": "aaa",
"teamtwo": "bbb",
"s_name": ""
}]
}


Instead i get something like this

{
"server_response": [{
"n_name": null,
"start": "2016-11-09 00:00:00",
"end": "2016-11-16 00:00:00",
"venue": "aaaaaa"
}],
"0": [{
"teamone": "aaa",
"teamtwo": "bbb",
"s_name": ""
}]
}


Can someone help me. Thanks!

Answer
$response = array();
while($row=mysqli_fetch_array($result))
{
    $response['server_response']["n_name"] = $row[1];
    $response['server_response']["start"] = $row[4];
    $response['server_response']["end"] = $row[5];
    $response['server_response']["venue"] = $row[6];
}


while($row=mysqli_fetch_array($result1))
{
    $response['server_response']["teamone"] = $row[1];
    $response['server_response']["teamtwo"] = $row[2];
    $response['server_response']["s_name"] = $row[10];
}

echo json_encode ($response);
Comments