Convert a PHPobject (SimpleXMLElement) object to an array-PHP source code

Source: Internet
Author: User
Convert the PHPobject (SimpleXMLElement) object to an array and jump to [1] [2] [full screen preview]
object(SimpleXMLElement)[9]  public 'result' =>     object(SimpleXMLElement)[8]            public 'err_code' => string '0' (length=1)      public 'model' => string '000000000000^000000000000' (length=26)      public 'success' => string 'true' (length=4)  public 'request_id' => string '000000000000' (length=13)

2.[Code]Example: how to convert a PHP object (SimpleXMLElement) object to an arrayJump to [1] [2] [full screen preview]

The class xml {/*** object (SimpleXMLElement) object is converted into an array * convert simplexml object to array sets * $ array_tags, which indicates the xml tag to be converted into an array. Example: array ('item ','') * if an error occurs, False is returned. ** @ param object $ simplexml_obj * @ param array $ array_tags * @ param int $ strip_white indicates whether to clear leading and trailing spaces * @ return mixed */public static function toArray ($ simplexml_obj, $ array_tags = array (), $ strip_white = 1) {if ($ simplexml_obj) {if (count ($ simplexml_obj) = 0) return $ strip_white? Trim (string) $ simplexml_obj) :( string) $ simplexml_obj; $ attr = array (); foreach ($ simplexml_obj as $ k => $ val) {if (! Empty ($ array_tags) & in_array ($ k, $ array_tags) {$ attr [] = self: toArray ($ val, $ array_tags, $ strip_white );} else {$ attr [$ k] = self: toArray ($ val, $ array_tags, $ strip_white) ;}} return $ attr;} return false ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.