简体   繁体   English

将结果提取到JSON数组PDO

[英]Fetch result to a json array PDO

im using fetchAll(PDO::FETCH_ASSOC) of the PDO class for fetching my results, then i take the result and run it through json_encode($results) which results in : 即时通讯使用PDO类的fetchAll(PDO::FETCH_ASSOC)来获取我的结果,然后获取结果并通过json_encode($results) ,结果为:

{ "0" : { "key" : "value" }, "1" : { "key" : "value" } }

But i was expecting : 但是我期待着:

 [ { "key" : "value" },{ "key" : "value" } ]

as Json array is more natural and reasonable then Json object in my case. 因为在我的案例中,Json数组比Json对象更为自然合理。

How would you go about either converting the json object to json array, or get the json array in the first place by using some other fetch style or any other suggestion? 您将如何将json对象转换为json数组,或者首先通过使用其他某些获取样式或任何其他建议来获取json数组?

Ok it heppened because i had some other type values in that json, something like : 好的,它突然出现了,因为我在json中还有其他一些类型值,例如:

{ "0" : { "key" : "value" }, "1" : { "key" : "value" } , "key" : [ { "key" : "value" } ]}

I just introduced a key for the array and it worked : 我刚刚介绍了数组的键,它起作用了:

{ "key" : [{ "key" : "value" },{ "key" : "value" }] , "key" : [ { "key" : "value" } ]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM