When PHP is working as a back-end server, it often encounters this situation. It needs to parse the xml file from the front-end and return the data in xml format. In this case, the conversion between xml and the associative array in PHP is Very frequent thing. For example, flex and other client programs often use this method to interact with the server. The following are two methods I have summarized, which greatly simplify the workload of converting xml and array.
[php]
/**
*
* Convert simple array into simple xml
* @param string $data Array to be converted
* @param string $tag The tag to use
* @example
* $arr = array(
'rtxAccount'=>'aaron','ipAddr'=>'192.168.0.12',
'conferenceList'=>array('conference'=>
array(
array('conferenceId'=>1212,'conferenceTitle'=>'quanshi 444','smeAccount'=>'bingxu.dong@quanshi.com'),
array('conferenceId'=>454,'conferenceTitle'=>'quanshi meeting','smeAccount'=>'bingxu.dong@quanshi.com'),
array('conferenceId'=>6767,'conferenceTitle'=>'quanshi meeting','smeAccount'=>'bingxu.dong@quanshi.com'),
array('conferenceId'=>232323,'conferenceTitle'=>'quanshi uuu','smeAccount'=>'bingxu.dong@quanshi.com'),
array('conferenceId'=>8989,'conferenceTitle'=>'quanshi meeting','smeAccount'=>'bingxu.dong@quanshi.com'),
array('conferenceId'=>1234343212,'conferenceTitle'=>'quanshi meeting','smeAccount'=>'bingxu.dong@quanshi.com')
)
)
);
Convert to:
*/
function array2xml($data,$tag = '')
{
$xml = '';
foreach($data as $key => $value)
{
if(is_numeric($key))
{
if(is_array($value))
{
$xml .= "<$tag>";
$xml .= array2xml($value);
$xml .="$tag>";
}
else
{
$xml .= "<$tag>$value$tag>";
}
}
else
{
if(is_array($value))
{
$keys = array_keys($value);
if(is_numeric($keys[0]))
{
$xml .=array2xml($value,$key);
}
else
{
$xml .= "<$key>";
$xml .=array2xml($value);
$xml .= "$key>";
}
}
else
{
$xml .= "<$key>$value$key>";
}
}
}
return $xml;
}
}
xml2array
[php]
/**
* *
* Convert simple xml into associative array
* @param string $xmlString xml string
* @example
*
& Lt; tel & gt; invited person 1 phone number & lt;/tel & gt;
& Lt; tel & gt; invited person 2 phone number & lt;/tel & gt;
Associative array after conversion:
Array
(
[conferenceTitle] => IT Exchange Conference 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
(
Since
(
[rtxAccount] => Invitee 1’s RTX account
[tel] => Invitee 1 phone number
)
[1] = & gt; Array
(
[rtxAccount] => Invitee 2’s RTX account
[tel] => Invitee 2 phone number
)
)
)
)
*/
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的专栏