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.
Copy CodeThe code is as follows:
/**
*
* 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 ' = ' http://www.jb51.net '),
Array (' Conferenceid ' =>454, ' conferencetitle ' = ' Quanshi meetting ', ' smeaccount ' = ' http://www.jb51.net '),
Array (' Conferenceid ' =>6767, ' conferencetitle ' = ' Quanshi meetting ', ' smeaccount ' = ' http://www.jb51.net '),
Array (' Conferenceid ' =>232323, ' conferencetitle ' = ' quanshi uuu ', ' smeaccount ' = ' http://www.jb51.net '),
Array (' Conferenceid ' =>8989, ' conferencetitle ' = ' Quanshi meetting ', ' smeaccount ' = ' http://www.jb51.net '),
Array (' Conferenceid ' =>1234343212, ' conferencetitle ' = ' Quanshi meetting ', ' smeaccount ' = ' http://www.jb51.net ')
)
)
);
Translate to:
Aaron
192.168.0.12
1212
Quanshi 444
Http://www.jb51.net
454
Quanshi meetting
Http://www.jb51.net
6767
Quanshi meetting
Http://www.jb51.net
232323
Quanshi UUU
Http://www.jb51.net
8989
Quanshi meetting
Http://www.jb51.net
1234343212
Quanshi meetting
Http://www.jb51.net
*/
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
Copy CodeThe code is as follows:
/**
*
* 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
[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;
}
http://www.bkjia.com/PHPjc/327794.html www.bkjia.com true http://www.bkjia.com/PHPjc/327794.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 ...