PHP converts XML into an array. php xml array _ PHP Tutorial

Source: Internet
Author: User
PHP converts XML into an array and a php xml array. PHP converts XML into an array. if you use the xmldataxmlsimplexmlloadstring (data) obtained by curl, data ['TK '] jsondecode (jsonencode (xml), TRUE ); how to Convert XML into an array using PHP directly?

If you use curl to obtain xml data
Xml = simplexmlloadstring (data );
Data ['TK '] = jsondecode (jsonencode (xml), TRUE );
If you directly obtain URL data
Xml = simplexmlloadfile (data );
Data ['TK '] = jsondecode (jsonencode (xml), TRUE );

Convert simplexml to json and then convert json to an array.

Code:

<?php$string = <<
  
   
   Forty What? 
   
    Joe
    
   
    Jane
     I know that's the answer -- but what's the question? 
  XML;$xml=simplexml_load_string($string);$data = json_decode(json_encode($xml),TRUE);var_dump( $xml );var_dump( $data );
 
object(SimpleXMLElement)[1] public 'title' => string 'Forty What?' (length=11) public 'from' => string 'Joe' (length=3) public 'to' => string 'Jane' (length=4) public 'body' => string ' I know that's the answer -- but what's the question? ' (length=57)array 'title' => string 'Forty What?' (length=11) 'from' => string 'Joe' (length=3) 'to' => string 'Jane' (length=4) 'body' => string ' I know that's the answer -- but what's the question? ' (length=57)

The above is all the content of this article, hoping to help you learn.

Articles you may be interested in:
  • Convert XML to an array in php
  • PHP code for converting arrays into XML format
  • Php-XML-to-array functions
  • PHP detailed explanation of converting XML to array
  • In php, the Array2xml class converts arrays into XML instances.
  • Php to convert arrays to XML
  • Recursive php array-to-xml code sharing

Export if you use curl to obtain xml data xml = simplexmlloadstring (data); data ['TK '] = jsondecode (jsonencode (xml), TRUE...

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.