Jackson Jackson - 5 months ago 8
MySQL Question

PHP: producing a json without {}?

I need to produce a php json like this using php from MYSQL database:

[['Staff Welfare', 'london', 'map-marker-icon.png'] , ['perfect', 'London', 'map-marker-icon.png'] , ['Fare Trade', 'essex', 'map-marker-icon.png'] , ['Fare Trade', 'london', 'map-marker-icon.png'] ,]


This is what I've tried:

$return_arr = array();
$sql = "SELECT * FROM restaurants";
if ($result = mysqli_query($db_conx, $sql )){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

$row_array['id'] = $row['id'];
$row_array['location'] = $row['location'];
$row_array['name'] = $row['name'];
$row_array['type'] = $row['type'];


array_push($return_arr,array($row_array));
}
}

mysqli_close($db_conx);

echo json_encode($return_arr);


But the above code echo's something like this:

[[{"id":"1","location":"london","name":"Staff","type":"indian"}], [{"id":"2","location":"London","name":"perfect","type":"chinese"}],[{"id":"3","location":"essex","name":"Trade","type":"kebab"}],[{"id":"5","location":"london","name":"Fare Trade","type":"kebab"}]]


could someone please let me know how I can remove the
{}
and also remove
column names
as well so my json echo's like what I am looking for?

Answer

You have to get rid of array keys to produce json without {}. Change the array_push code to this:

array_push($return_arr, array_values($row));

array_values function will return only $row values without keys.