gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:48:56 25 4
gpt4 key购买 nike

根据我的经验,当我对 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"],
] }

最佳答案

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

$data = array();

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

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

关于PHP json_encode 将行作为数组而不是对象返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6644130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com