dongruoqiong9017 2016-06-11 11:22
浏览 28
已采纳

PHP:生成一个没有{}的json?

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?

  • 写回答

2条回答 默认 最新

  • doubao6681 2016-06-11 11:30
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)