Home > Backend Development > PHP Tutorial > php中将SimpleXMLElement Object数组转化为普通数组

php中将SimpleXMLElement Object数组转化为普通数组

WBOY
Release: 2016-06-23 13:17:09
Original
1033 people have browsed it

做微信开发,鉴于微信POST的消息是XML数据包,通过SimpleXMLElement Object获取的数据不好操作,需要转化为普通数组。

网上找了很多方法都不理想,发现通过json_decode和json_encode可以转化,遂分享给大家。

$postStr = '<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName>  <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[this is a test]]></Content> <MsgId>1234567890123456</MsgId> </xml>';$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);$jsonStr = json_encode($postObj);$jsonArray = json_decode($jsonStr,true);print_r($jsonArray);
Copy after login

输出结果为:

Array(    [ToUserName] => toUser    [FromUserName] => fromUser    [CreateTime] => 1348831860    [MsgType] => text    [Content] => this is a test    [MsgId] => 1234567890123456)
Copy after login

这样操作起来就容易多了。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template