PHP XML conversion Array function _php Tutorial

Source: Internet
Author: User
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.html www.bkjia.com true http://www.bkjia.com/PHPjc/486203.html techarticle PHP XML conversion to an array function? 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.