PHP in the background server, often encounter this situation, need to parse the XML file from the foreground, and return the data in XML format, in this case, XML and PHP associative array conversion is very frequent things. For example, flex and other client programs interact with the server, often using this approach. Here are my two methods, which greatly simplifies the conversion of XML to arrays.
[PHP]
/**
*
* Convert simple arrays to simple XML
* @param string $data the array to be converted
* @param string $tag the label to use
* @example
* $arr = Array (
' Rtxaccount ' = ' Aaron ', ' ipaddr ' and ' 192.168.0.12 ',
' Conferencelist ' =>array (' conference ' =
Array
Array (' Conferenceid ' =>1212, ' conferencetitle ' = ' Quanshi 444 ', ' smeaccount ' = ' bingxu.dong@quanshi.com '),
Array (' Conferenceid ' =>454, ' conferencetitle ' = ' Quanshi meetting ', ' smeaccount ' = ' bingxu.dong@quanshi.com ' '),
Array (' Conferenceid ' =>6767, ' conferencetitle ' = ' Quanshi meetting ', ' smeaccount ' = ' bingxu.dong@ ' Quanshi.com '),
Array (' Conferenceid ' =>232323, ' conferencetitle ' = ' quanshi uuu ', ' smeaccount ' = ' bingxu.dong@quanshi.com ') ,
Array (' Conferenceid ' =>8989, ' conferencetitle ' = ' Quanshi meetting ', ' smeaccount ' = ' bingxu.dong@ ' Quanshi.com '),
Array (' Conferenceid ' =>1234343212, ' conferencetitle ' = ' Quanshi meetting ', ' smeaccount ' = ' bingxu.dong@ ' Quanshi.com ')
)
)
);
Translate to:
Aaron
192.168.0.12
1212
Quanshi 444
Bingxu.dong@quanshi.com
454
Quanshi meetting
Bingxu.dong@quanshi.com
6767
Quanshi meetting
Bingxu.dong@quanshi.com
232323
Quanshi UUU
Bingxu.dong@quanshi.com
8989
Quanshi meetting
Bingxu.dong@quanshi.com
1234343212
Quanshi meetting
Bingxu.dong@quanshi.com
*/
function Array2xml ($data, $tag = ")
{
$xml = ";
foreach ($data as $key = $value)
{
if (Is_numeric ($key))
{
if (Is_array ($value))
{
$xml. = "< $tag >";
$xml. = Array2xml ($value);
$xml. = " ";
}
Else
{
$xml. = "< $tag > $value ";
}
}
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. = " ";
}
}
Else
{
$xml. = "< $key > $value ";
}
}
}
return $xml;
}
}
Xml2array
[PHP]
/**
*
* Convert simple XML into associative arrays
* @param string $xmlString XML string
* @example
*
It Exchange
2011-12-19 12:00:00
andy1111111
192.168.1.56
120
1
1 of the invited person's RTX account
Invitee 1 Phone number
2 of the invited person's RTX account
Invitee 2 Phone number
The associative array after conversion:
Array
(
[Conferencetitle] = It exchange 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] = invitee 1 of the RTX account
[Tel] = invitee 1 Phone number
)
[1] = = Array
(
[Rtxaccount] = invitee 2 of the 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;
}
Excerpt from Andy1219111 's column
http://www.bkjia.com/PHPjc/478300.html www.bkjia.com true http://www.bkjia.com/PHPjc/478300.html techarticle PHP in the background server, often encounter this situation, need to parse the XML file from the foreground, and return the data in XML format, in this case, XML and PHP associative array ...