PHP XML转换为数组函数_PHP教程
程序员文章站
2024-01-28 10:12:58
...
PHP XML转换为数组函数
- function xml2array($originalXML, $attributes=true)
- {
- $xmlArray = array();
- $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; - // normalize data
- $xml = preg_replace(|>s*
- $xml = preg_replace(|.*?>|, , $xml); // remove XML declarations
- $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 - 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);
- &nb
下一篇: php判断数组相等数组运算符
推荐阅读
-
PHP数组函数array_walk()笔记_PHP教程
-
php数组函数序列之in_array() 查找数组值是否存在_PHP教程
-
php数组函数序列之array_combine() - 数组合并函数使用说明_PHP教程
-
php中的注释、变量、数组、常量、函数应用介绍_PHP教程
-
PHP数组函数array_map()笔记_PHP教程
-
php数组函数序列之array_keys() - 获取数组键名_PHP教程
-
数组的键/值操作函数,数组函数_PHP教程
-
经验之谈PHP数组函数总结_PHP教程
-
PHP实现多维数组转字符串和多维数组转一维数组的方法,多维维数_PHP教程
-
php数组函数序列之array_values() 获取数组元素值的函数与方法_PHP教程