douminfu8033 2014-06-26 22:11
浏览 146

从mysql查询生成json数组

I am trying the following code:

$order = array(); $imageURL = array(); $name = array();
        while($row = mysql_fetch_array($mysql->result)) {
            $order[] = $row["order"]; 
            $imageURL[] = $row["imageURL"];
            $name[] = $row["name"];
        }
        $res = array($order, $imageURL,$name);
        return json_encode($res);

But it is not outputting in json format, any ideas?

Output:

[["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"],["previews\/en-1-1.gif","previews\/en-1-2.gif","previews\/en-1-3.gif","previews\/en-1-4.gif","previews\/en-1-5.gif","previews\/en-1-6.gif","previews\/en-1-7.gif","previews\/en-1-8.gif","previews\/en-1-9.gif","previews\/en-1-10.gif","previews\/en-1-11.gif","previews\/en-1-12.gif","previews\/en-1-13.gif","previews\/en-1-14.gif","previews\/en-1-15.gif","previews\/en-1-16.gif","previews\/en-1-17.gif","previews\/en-1-18.gif","previews\/en-1-19.gif","previews\/en-1-20.gif","previews\/en-1-21.gif","previews\/en-1-22.gif","previews\/en-1-23.gif","previews\/en-1-24.gif","previews\/en-1-25.gif","previews\/en-1-26.gif","previews\/en-1-27.gif","previews\/en-1-28.gif","previews\/en-1-29.gif","previews\/en-1-30.gif"],["Helasd you?","Where sasaddsdam?","Weasdd!","Tasasdther","AtsaddsaBeach","Cheasd Hotel","At the Hotel","aaaaaaaaat?","At the Market","Aasdt's","Mesadasd th","Shdsdsssg","sssss","On aaaa","Do you work or study?","aaaaaaa","At tadstation","aaaaaae Gym","How doasdto\u2026?","Planning a Trip","At adsk","At the asdurant","At the Inads00e9","My Taaaog","A Meetiaaaaay adher","Tourist sdsadn Centre","Saaaaing","Aaaaaa Match","Lookaaasd","At tasda"]]
  • 写回答

3条回答 默认 最新

  • dopcpc9207 2014-06-26 23:01
    关注

    I think you're just after a better format to work with in your output, because you've currently set it up with three arrays and you'll have to reference each record's properties across these arrays by a numeric key.

    I've reconstructed an example of what your original database output would look like:

    $array = json_decode($json, true);
    
    $row = array();
    list($orders, $imageURLS, $names) = $array;
    foreach($orders as $key => $val) {
      $row[] = array(
        'order' => $val,
        'imageURL' => $imageURLS[$key],
        'name' => $names[$key]
      );
    }
    

    So in your code, you should try this:

    $output = array();
    while($row = mysql_fetch_array($mysql->result)) {
        $output[] = $row;
    }
    return json_encode($output);
    

    And you'll get a much easier data structure to work with. Example:

    [
      {
        "order": "1",
        "imageURL": "previews\/en-1-1.gif",
        "name": "Helasd you?"
      },
      {
        "order": "2",
        "imageURL": "previews\/en-1-2.gif",
        "name": "Where sasaddsdam?"
      }
    ]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数