How to filter data when xml is converted to json? JSON XML
The xml below is converted to json
But under the P node, only The data of these two nodes is not required for other data.
If you use json_encode to transfer out all the data
How can I convert it to json, but as long How to get the data?
For example
{"PN": "\ u7b2c \ u4e00 \ u5468 \ u64ad \ u5267 \ u573a \ uff1a \ u8ffd \ u9c7c \ u4f20 \ u5947 31", "PT": "22:01:00 "}
------- Xml ---------
110171675
Zhang San
2013-08-19 00:02:00
46
24
74750
0
2013-08-19 00:02:00
AM
110171676
I am a beauty
2013-08-19 01:15:00
46
24
74501
0
2013-08-19 01:15:00
AM
110171677
Li Si
2013-08-19 02:09:00
46
24
64519
71411
2013-08-19 02:09:00
AM
Reply to discussion (solution)
$ String = <
110171675
Zhang San
2013-08-19 00:02:00
46
24
74750
0
2013-08-19 00:02:00
AM
110171676
I am a beauty
2013-08-19 01:15:00
46
24
74501
0
2013-08-19 01:15:00
AM
110171677
Li Si
2013-08-19 02:09:00
46
24
64519
71411
2013-08-19 02:09:00
AM
XML; $ xml = simplexml_load_string ($ string); foreach ($ xml-> P as $ item) {$ item = (array) $ item; $ arr [] = array ('pn '=> $ item ['pn'], 'pt' => $ item ['pt']);} echo json_encode ($ arr );
[{"PN": "\ u5f20 \ u4e09", "PT": "00:02:00" },{ "PN ": "\ u6211 \ u662f \ u5927 \ u7f8e \ u4eba", "PT": "01:15:00" },{ "PN": "\ u674e \ u56db", "PT ": "2013-08-19 02:09:00"}]
$ Xml = <XML
110171675
Zhang San
2013-08-19 00:02:00
46
24
74750
0
2013-08-19 00:02:00
AM
110171676
I am a beauty
2013-08-19 01:15:00
46
24
74501
0
2013-08-19 01:15:00
AM
110171677
Li Si
2013-08-19 02:09:00
46
24
64519
71411
2013-08-19 02:09:00
AM
XML; $ sm = simplexml_load_string ($ xml); foreach ($ sm-> P as $ item) {$ r [] = array ('pn '=> strval ($ item-> PN), 'pt' => strval ($ item-> PT ));} echo json_encode ($ r );
[{"PN": "\ u5f20 \ u4e09", "PT": "00:02:00" },{ "PN ": "\ u6211 \ u662f \ u5927 \ u7f8e \ u4eba", "PT": "01:15:00" },{ "PN": "\ u674e \ u56db", "PT ": "2013-08-19 02:09:00"}]