时间:2021-07-01 10:21:17 帮助过:2人阅读
xml2array
[php]
/**
*
* 将简单的xml转化成关联数组
* @param string $xmlString xml字符串
* @example
*
转化之后的关联数组:
Array
(
[conferenceTitle] => IT交流会 www.2cto.com
[startTime] => 2011-12-19 12:00:00
[rtxAccount] => andy1111111
[ipAddr] => 192.168.1.56
[duration] => 120
[conferenceType] => 1
[invitees] => Array
(
[invitee] => Array
(
[0] => Array
(
[rtxAccount] => 被邀请人1的RTX账号
[tel] => 被邀请人1电话号码
)
[1] => Array
(
[rtxAccount] => 被邀请人2的RTX账号
[tel] => 被邀请人2电话号码
)
)
)
)
*/
function xml2array($xmlString = '')
{
$targetArray = array();
$xmlObject = simplexml_load_string($xmlString);
$mixArray = (array)$xmlObject;
foreach($mixArray as $key => $value)
{
if(is_string($value))
{
$targetArray[$key] = $value;
}
if(is_object($value))
{
$targetArray[$key] = xml2array($value->asXML());
}
if(is_array($value))
{
foreach($value as $zkey => $zvalue)
{
if(is_numeric($zkey))
{
$targetArray[$key][] = xml2array($zvalue->asXML());
}
if(is_string($zkey))
{
$targetArray[$key][$zkey] = xml2array($zvalue->asXML());
}
}
}
}
return $targetArray;
}
摘自 andy1219111的专栏
http://www.bkjia.com/PHPjc/478300.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478300.htmlTechArticlephp在做后台服务器的时候,经常会遇到这种情况,需要解析来自前台的xml文件,并将数据以xml格式返回,在这种情况下,xml与php中关联数组...