欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP XML转换为数组函数

程序员文章站 2024-01-15 15:55:22
...
PHP XML转换为数组函数

  1. function xml2array($originalXML, $attributes=true)
  2. {
  3. $xmlArray = array();
  4. $search = $attributes ? |(.*) function xml2array($originalXML, $attributes=true)
    {
    $xmlArray = array();
    $search = $attributes ? |(.*)2>|Ums : |(.*)2>|Ums;

    // normalize data
    $xml = preg_replace(|>s* $xml = preg_replace(|.*?>|, , $xml); // remove XML declarations
    $xml = preg_replace(||U, $1>, $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 : |(.*) function xml2array($originalXML, $attributes=true)
    {
    $xmlArray = array();
    $search = $attributes ? |(.*)2>|Ums : |(.*)2>|Ums;

    // normalize data
    $xml = preg_replace(|>s* $xml = preg_replace(|.*?>|, , $xml); // remove XML declarations
    $xml = preg_replace(||U, $1>, $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*
  7. $xml = preg_replace(|.*?>|, , $xml); // remove XML declarations
  8. $xml = preg_replace(||U, function xml2array($originalXML, $attributes=true)
    {
    $xmlArray = array();
    $search = $attributes ? |(.*)2>|Ums : |(.*)2>|Ums;

    // normalize data
    $xml = preg_replace(|>s* $xml = preg_replace(|.*?>|, , $xml); // remove XML declarations
    $xml = preg_replace(||U, $1>, $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;
    }
    ?>>[code]>, $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