I have an issue regarding the API JSON output. I am getting stuck into JSON output when in a loop, it is making its own index when i am forcing the array format to change into the index array.
PHP Code:
$sql = "SELECT name,fname,address FROM user_management";
$array = array();
$result = $conn->query($sql);
$i=1;
if ($result->num_rows > 0) {
// output data of each row
$array=["message"=>"success"];
while($row = $result->fetch_assoc()) {
$array[] = ["Hello" => $row];
}
}
else
{
echo "0 results";
}
//echo implode(',', $array);
// Take output array glue it with the
echo json_encode($array);
$conn->close();
?>
I am getting the response as:
{
"message": "success",
"0": {
"Hello": {
"name": "harjot",
"fname": "tejinder",
"address": "noida"
}
},
"1": {
"Hello": {
"name": "regret",
"fname": "egegrregeger",
"address": "gegreg"
}
},
"2": {
"Hello": {
"name": "harjot1",
"fname": "harjot2",
"address": "noida"
}
},
"3": {
"Hello": {
"name": "har",
"fname": "har1",
"address": "Punjab"
}
}
}
I want the response as without the 0,1,2,3 that array is making on its own.Is there a way to access the index of an arrays?
Expected output:
{ "message": "success",
"Hello": {
"name": "harjot",
"fname": "tejinder",
"address": "noida" },
"Hello1": {
"name": "regret",
"fname": "egegrregeger",
"address": "gegreg" },
"Hello2": {
"name": "harjot1",
"fname": "harjot2",
"address": "noida" },
"Hello3": {
"name": "har",
"fname": "har1",
"address": "Punjab"
}
}