PHP json_encode 将行返回为数组而不是对象

发布于 2024-11-19 12:04:51 字数 379 浏览 0 评论 0原文

根据我的经验,当我对 mysql 表中的数据进行 json_encode 时,输出是一个对象,其中包含每一行作为对象。我想要做的是将每一行作为数组,如下例所示(不需要其中的列名称)。我怎样才能做到这一点?

{ "Data": [
    ["1","Internet Explorer 4.0","Win 95+","4","X","1"],
    ["2","Internet Explorer 5.0","Win 95+","5","C","2"],
    ["3","Internet Explorer 5.5","Win 95+","5.5","A","3"],
    ["4","Internet Explorer 6","Win 98+","6","A","4"],
] }

From my experience, when I json_encode data from a mysql table, the output is an object containing each row as an object within. What I want to do is to get each row as an array, like the example below (don't need the column names in there). How can I do that?

{ "Data": [
    ["1","Internet Explorer 4.0","Win 95+","4","X","1"],
    ["2","Internet Explorer 5.0","Win 95+","5","C","2"],
    ["3","Internet Explorer 5.5","Win 95+","5.5","A","3"],
    ["4","Internet Explorer 6","Win 98+","6","A","4"],
] }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙与沫 2024-11-26 12:04:51

使用 mysql_fetch_row [docs] 获取每一行。这将获得一行作为数字数组,并且这些行也被编码为 JSON 数组:

$data = array();

while(($row = mysql_fetch_row($result))) {
    $data[] = $row;
}

echo json_encode(array('Data' => $data));

Use mysql_fetch_row [docs] to get each row. This will get a row as a numerical array and those are also encoded as JSON arrays:

$data = array();

while(($row = mysql_fetch_row($result))) {
    $data[] = $row;
}

echo json_encode(array('Data' => $data));
粉红×色少女 2024-11-26 12:04:51

口译员未检查:
您可以通过简单的类型转换来更改变量的类型:

$data = DB::get('SELECT * FROM `table` WHERE 1');
echo json_encode(array('Data' => (array)$data));

您也可以使用下一个函数: array mysql_fetch_array (resource $result [, int $result_type ] )

NOT CHECKED in interpreter:
You can change the type of variable by simple types convertion:

$data = DB::get('SELECT * FROM `table` WHERE 1');
echo json_encode(array('Data' => (array)$data));

Also you can use next function: array mysql_fetch_array ( resource $result [, int $result_type ] )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文