JSON XML
下边的xml,想转为json
$string = <<<XML<Root> <P> <id>110171675</id> <PN>张三</PN> <PT>2013-08-19 00:02:00</PT> <Cd>46</Cd> <Td>24</Td> <Fd>74750</Fd> <Fd2>0</Fd2> <cPT>2013-08-19 00:02:00</cPT> <AMPM>AM</AMPM> <ls/> </P> <P> <id>110171676</id> <PN>我是大美人</PN> <PT>2013-08-19 01:15:00</PT> <Cd>46</Cd> <Td>24</Td> <Fd>74501</Fd> <Fd2>0</Fd2> <cPT>2013-08-19 01:15:00</cPT> <AMPM>AM</AMPM> <ls/> </P> <P> <id>110171677</id> <PN>李四</PN> <PT>2013-08-19 02:09:00</PT> <Cd>46</Cd> <Td>24</Td> <Fd>64519</Fd> <Fd2>71411</Fd2> <cPT>2013-08-19 02:09:00</cPT> <AMPM>AM</AMPM> <ls/> </P> </Root>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);
$xml =<<< XML<Root> <P> <id>110171675</id> <PN>张三</PN> <PT>2013-08-19 00:02:00</PT> <Cd>46</Cd> <Td>24</Td> <Fd>74750</Fd> <Fd2>0</Fd2> <cPT>2013-08-19 00:02:00</cPT> <AMPM>AM</AMPM> <ls/> </P> <P> <id>110171676</id> <PN>我是大美人</PN> <PT>2013-08-19 01:15:00</PT> <Cd>46</Cd> <Td>24</Td> <Fd>74501</Fd> <Fd2>0</Fd2> <cPT>2013-08-19 01:15:00</cPT> <AMPM>AM</AMPM> <ls/> </P> <P> <id>110171677</id> <PN>李四</PN> <PT>2013-08-19 02:09:00</PT> <Cd>46</Cd> <Td>24</Td> <Fd>64519</Fd> <Fd2>71411</Fd2> <cPT>2013-08-19 02:09:00</cPT> <AMPM>AM</AMPM> <ls/> </P> </Root>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);