PHP parses XML into an array

Source: Internet
Author: User
PHP parses XML into arrays online and other XMLcode & lt ;? Xmlversion = & quot; 1.0 & quot; encoding = & quot; UTF-8 & quot ;? & Gt; & lt; root & gt; & lt; a & gt; & lt; bname = 'name' value PHP parses XML into an array online, etc.
XML code
  
  
           
         
         
               
         
                     
         
         
        
      
  

Resolved to an array:
PHP code
      Array(       [0]=>Array(            [0]=>Array(name=>'name',value=>'value'),            [1]=>Array(name=>'name',value=>'value'),            [2]=>Array(name=>'name',value=>'value')       ),       [1]=>Array(         [0]=>Array(name=>'name',value=>'value'),         [1]=>Array(name=>'name',value=>'value'),         [2]=>Array(name=>'name',value=>'value')       ),       [2]=>Array(         [0]=>Array(name=>'name',value=>'value'),         [1]=>Array(name=>'name',value=>'value'),         [2]=>Array(name=>'name',value=>'value')      ),)


------ Solution --------------------
PHP code
  
           
         
         
               
         
                     
         
         
        
      
  XML; $ obj = simplexml_load_string ($ xml); foreach ($ obj as $ k = >$ v) {$ temp = array (); foreach ($ v-> B as $ sv) {$ a = (array) $ sv-> attributes (); $ temp [] = $ a ['@ bubutes'];} $ arr [] = $ temp;} print_r ($ arr );
------ Solution --------------------
PHP code
$s =<<< XML
    
             
           
           
                 
           
                 
           
           
          
        
    XML;$xml = simplexml_load_string($s);foreach($xml as $item1) {  $r = array();  foreach($item1 as $item2) {    $t = array();    foreach($item2->attributes() as $k=>$v) {      $t[] = array($k => "$v");    }    $r[] = $t;  }  $res[] = $r;}print_r($res);

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.