带有json_encode的PDO将数据作为数组返回 [英] PDO with json_encode return data as arrays

查看:66
本文介绍了带有json_encode的PDO将数据作为数组返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试将数据作为二维数组而不是单个数组中的对象返回时遇到问题.

I'm having an issue with trying to return my data as 2 dimensional arrays rather than objects in a single array.

我这样获取查询结果:

    if($stmt->rowCount()){
        echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC)); exit;  
    }

我从json_encode获得的数据是这样的:

The data I get from the json_encode is like this:

   [
      {"id":"1","name":"Test"},{"id":"2","name":"Test 2"}
   ]

我希望如何以这种格式返回数据:

How ever i am looking to get the data returned in this format:

[0] 
  [0] = "1";
  [1] = "Test";
[1]
  [0] = "2";
  [1] = "Test 2";

PDO在获取结果时是否提供这种格式?

Does PDO provide such a format when fetching results?

推荐答案

echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));更改为echo json_encode($stmt->fetchAll(PDO::FETCH_NUM));

http://php.net/manual/en/pdostatement.fetch.php

这篇关于带有json_encode的PDO将数据作为数组返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆