Php 当作为JSON返回时,如何保持数组结构不变?
我有一个问题,我有一些数据的多维数组,我通过API以JSON的形式返回,我希望数据是JSON数组,数组在PHP中的结构是正确的,但是当我通过API以JSON的形式返回它时,它变成了对象的JSON对象,我希望数据的结构与我在生成数组时在PHP中保存的结构相同。我尝试将对象转换为数组,数组在PHP中是完美的,但在JSON中它变成了对象的对象。如何以数组的形式返回JSON数据Php 当作为JSON返回时,如何保持数组结构不变?,php,json,api,decode,encode,Php,Json,Api,Decode,Encode,我有一个问题,我有一些数据的多维数组,我通过API以JSON的形式返回,我希望数据是JSON数组,数组在PHP中的结构是正确的,但是当我通过API以JSON的形式返回它时,它变成了对象的JSON对象,我希望数据的结构与我在生成数组时在PHP中保存的结构相同。我尝试将对象转换为数组,数组在PHP中是完美的,但在JSON中它变成了对象的对象。如何以数组的形式返回JSON数据 { "standards":{ "standard_1":{ "subjects":{
{
"standards":{
"standard_1":{
"subjects":{
"English":{
"topics":[
]
},
"Physics":{
"topics":{
"topic":[
{
"id":"2",
"standard_id":"1",
"subject_id":"3",
"topic":"topic",
"title":"dgfdgfdg",
"option_1":"dfgdfggdgfdg",
"option_2":"dfgfd",
"option_3":"gdfgfdg",
"option_4":"dfgdfgdg",
"correct_answer":"option_2"
}
],
"test topic":[
{
"id":"6",
"standard_id":"1",
"subject_id":"3",
"topic":"test topic",
"title":"title",
"option_1":"1",
"option_2":"2",
"option_3":"3",
"option_4":"4",
"correct_answer":"option_2"
}
]
}
},
"sports":{
"topics":[
]
},
"Maths":{
"topics":{
"topic":[
{
"id":"1",
"standard_id":"1",
"subject_id":"21",
"topic":"topic",
"title":"This is a updated test question",
"option_1":"1",
"option_2":"2",
"option_3":"3",
"option_4":"4",
"correct_answer":"option_2"
}
]
}
},
"test subject":{
"topics":[
]
}
}
},
"standard_2":{
"subjects":{
"Math":{
"topics":[
]
},
"Chemistry":{
"topics":{
"topic":[
{
"id":"4",
"standard_id":"2",
"subject_id":"4",
"topic":"topic",
"title":"sfsfsfs",
"option_1":"fdsfs",
"option_2":"fsfs",
"option_3":"fsdfsdf",
"option_4":"sdfdsfds",
"correct_answer":"option_1"
}
]
}
},
"test sub":{
"topics":{
"fghgfh":[
{
"id":"3",
"standard_id":"2",
"subject_id":"19",
"topic":"fghgfh",
"title":"Title",
"option_1":"fhfhf",
"option_2":"hfhf",
"option_3":"hgfhf",
"option_4":"fhfghfhfh",
"correct_answer":"option_4"
}
]
}
}
}
},
"standard_3":{
"subjects":{
"Sports":{
"topics":[
]
}
}
},
"standard_4":{
"subjects":{
"new subject":{
"topics":[
]
},
"Computer":{
"topics":{
"Basics":[
{
"id":"7",
"standard_id":"4",
"subject_id":"24",
"topic":"Basics",
"title":"What is a computer",
"option_1":"Machine",
"option_2":"Aeroplane",
"option_3":"Device",
"option_4":"Robot",
"correct_answer":"option_1"
}
]
}
}
}
},
"standard_5":{
"subjects":{
"Math":{
"topics":[
]
},
"":{
"topics":[
]
},
"History":{
"topics":[
]
}
}
},
"standard_6":{
"subjects":[
]
},
"standard_7":{
"subjects":{
"Sports":{
"topics":[
]
},
"Civics":{
"topics":[
]
}
}
},
"standard_8":{
"subjects":[
]
},
"standard_9":{
"subjects":[
]
},
"standard_10":{
"subjects":{
"History":{
"topics":[
]
},
"Math":{
"topics":[
]
}
}
},
"standard_11":{
"subjects":[
]
},
"standard_12":{
"subjects":[
]
}
}
}
[standard_1] => Array
(
[subjects] => Array
(
[English] => Array
(
[topics] => Array
(
)
)
[Physics] => Array
(
[topics] => Array
(
[topic] => Array
(
[0] => stdClass Object
(
[id] => 2
[standard_id] => 1
[subject_id] => 3
[topic] => topic
[title] => dgfdgfdg
[option_1] => dfgdfggdgfdg
[option_2] => dfgfd
[option_3] => gdfgfdg
[option_4] => dfgdfgdg
[correct_answer] => option_2
)
)
[test topic] => Array
(
[0] => stdClass Object
(
[id] => 6
[standard_id] => 1
[subject_id] => 3
[topic] => test topic
[title] => title
[option_1] => 1
[option_2] => 2
[option_3] => 3
[option_4] => 4
[correct_answer] => option_2
)
)
)
)
[sports] => Array
(
[topics] => Array
(
)
)
[Maths] => Array
(
[topics] => Array
(
[topic] => Array
(
[0] => stdClass Object
(
[id] => 1
[standard_id] => 1
[subject_id] => 21
[topic] => topic
[title] => This is a updated test question
[option_1] => 1
[option_2] => 2
[option_3] => 3
[option_4] => 4
[correct_answer] => option_2
)
)
)
)
[test subject] => Array
(
[topics] => Array
(
)
)
)
)
有什么想法吗?你要找的可能是
json\u解码($jsonString,true)代码>。它将把json中的对象转换成数组如果您希望json对象在php中作为数组返回,只需使用json\u decode($json,true)代码>
如果您想确保您的php数组正确转换为json,请使用json\u encode($phpArray,json\u FORCE\u OBJECT)
我们可以使用数组结构吗?当然,我已经更新了这个问题。如果您不喜欢json中的{}
格式,然后,您需要将PHP数组保持为不带键的值的集合(PHP内部管理的数字键除外)。您得到的JSON格式正确,当客户端使用JSON\u decode($API\u DATA,true)时,将使用此格式的客户端在其PHP后端上不会有问题
正如@vivek_23提到的键一样,对于您的示例数据,您必须去掉标准
(第一个对象键),然后删除标准
标准*
等的所有键名,以便每个值都位于数组[0][1]等中。如果您想保留标准
等。。。那么你所要求的是不可能的。你要么有一个枚举的值数组,要么有一个键名及其值的列表。谢谢,但我不会在PHP中再次解析JSON对象。所以真正的问题不是将JSON解码为数组,而是强制JSON_encode将对象转换为数组?我想你需要澄清一下你的问题。是的,你明白了,我希望JSON是一个数组而不是对象。那么这个问题就没有解决方案了。JSON在设计上有列表(没有键)和对象(支持键)。你可以看到你的json包含键(第一个是“标准”),这使得json转换器将其作为一个对象。事实并非如此,你可以参考上面的评论来清楚地了解我的问题,谢谢:)正如janmy在那里说的。这是因为您的php数组传递给了json\u encode
函数。因此,如果需要,可以更改php数组。如果不能,请在生成时使用它。:-)