Php xml conversion to array function _ PHP Tutorial

Source: Internet
Author: User
Convert PHPXML to an array function. Convert PHPXML to an array function? Phpfunctionxml2array ($ originalXML, $ attributestrue) {$ xmlArrayarray (); $ search $ attributes? | (S) (. *) s *(.*)? Phpfunctionxml2array ($ origin php xml conversion to an array function

  1. Function xml2array ($ originalXML, $ attributes = true)
  2. {
  3. $ XmlArray = array ();
  4. $ Search = $ attributes? | <(S) (. *) s *> (.*) Function xml2array ($ originalXML, $ attributes = true)
    {
    $ XmlArray = array ();
    $ Search = $ attributes? | <(S) (. *) s *> (.*) | Ums: | <(S) (). *> (.*) | Ums;

    // Normalize data
    $ Xml = preg_replace (|> s * <|, "> <", $ originalXML); // one tag per line
    $ Xml = preg_replace (| |, $ Xml); // remove XML declarations
    $ Xml = preg_replace (| <(S ?) (. *)/> | U, <$1 $2> , $ Xml); // Expand singletons

    If (! Preg_match_all ($ search, $ xml, $ xmlMatches ))
    Return trim ($ originalXML); // bail out-no XML found

    Foreach ($ xmlMatches [1] as $ index => $ key)
    {
    If (! Isset ($ xmlArray [$ key]) $ xmlArray [$ key] = array ();
    $ XmlArray [$ key] [] = xml2array ($ xmlMatches [4] [$ index], $ attributes );
    }
    Return $ xmlArray;
    }
    ?> | Ums: | <(S) (). *> (.*) Function xml2array ($ originalXML, $ attributes = true)
    {
    $ XmlArray = array ();
    $ Search = $ attributes? | <(S) (. *) s *> (.*) | Ums: | <(S) (). *> (.*) | Ums;

    // Normalize data
    $ Xml = preg_replace (|> s * <|, "> <", $ originalXML); // one tag per line
    $ Xml = preg_replace (| |, $ Xml); // remove XML declarations
    $ Xml = preg_replace (| <(S ?) (. *)/> | U, <$1 $2> , $ Xml); // Expand singletons

    If (! Preg_match_all ($ search, $ xml, $ xmlMatches ))
    Return trim ($ originalXML); // bail out-no XML found

    Foreach ($ xmlMatches [1] as $ index => $ key)
    {
    If (! Isset ($ xmlArray [$ key]) $ xmlArray [$ key] = array ();
    $ XmlArray [$ key] [] = xml2array ($ xmlMatches [4] [$ index], $ attributes );
    }
    Return $ xmlArray;
    }
    ?> | Ums;
  5. // Normalize data
  6. $ Xml = preg_replace (|> s * <|, "> <", $ originalXML); // one tag per line
  7. $ Xml = preg_replace (| |, $ Xml); // remove XML declarations
  8. $ Xml = preg_replace (| <(S ?) (. *)/> | U, <[code] Function xml2array ($ originalXML, $ attributes = true)
    {
    $ XmlArray = array ();
    $ Search = $ attributes? | <(S) (. *) s *> (.*) | Ums: | <(S) (). *> (.*) | Ums;

    // Normalize data
    $ Xml = preg_replace (|> s * <|, "> <", $ originalXML); // one tag per line
    $ Xml = preg_replace (| |, $ Xml); // remove XML declarations
    $ Xml = preg_replace (| <(S ?) (. *)/> | U, <$1 $2> , $ Xml); // Expand singletons

    If (! Preg_match_all ($ search, $ xml, $ xmlMatches ))
    Return trim ($ originalXML); // bail out-no XML found

    Foreach ($ xmlMatches [1] as $ index => $ key)
    {
    If (! Isset ($ xmlArray [$ key]) $ xmlArray [$ key] = array ();
    $ XmlArray [$ key] [] = xml2array ($ xmlMatches [4] [$ index], $ attributes );
    }
    Return $ xmlArray;
    }
    ?> , $ Xml); // Expand singletons
  9. If (! Preg_match_all ($ search, $ xml, $ xmlMatches ))
  10. Return trim ($ originalXML); // bail out-no XML found
  11. Foreach ($ xmlMatches [1] as $ index => $ key)
  12. {
  13. If (! Isset ($ xmlArray [$ key]) $ xmlArray [$ key] = array ();
  14. $ XmlArray [$ key] [] = xml2array ($ xmlMatches [4] [$ index], $ attributes );
  15. & Nb

    Http://www.bkjia.com/PHPjc/486203.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/486203.htmlTechArticlePHP XML conversion to array functions? Php function xml2array ($ originalXML, $ attributes = true) {$ xmlArray = array (); $ search = $ attributes? | (S) (. *) s *(.*)/? Php function xml2array ($ origin...

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.