Discussion: Array2xml and Xml2array, and _php examples of XML and array conversion

Source: Internet
Author: User

PHP in the background server, often encounter this situation, need to parse the XML file from the front desk, and the data returned 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 the two methods I've summed up, which greatly simplifies the effort to transform XML from one array to another.

Copy Code code as follows:

/**
*
* Convert a simple array into a simple XML
* @param string $data the array to be converted
* @param string $tag the label to use
* @example
* $arr = Array (
' Rtxaccount ' => ' Aaron ', ' ipaddr ' => ' 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 ')
)
)
);
Converted to:
<rtxAccount>aaron</rtxAccount>
<ipAddr>192.168.0.12</ipAddr>
<conferenceList>
<conference>
<conferenceId>1212</conferenceId>
<conferencetitle>quanshi 444</conferencetitle>
<smeAccount>http://www.jb51.net</smeAccount>
</conference>
<conference>
<conferenceId>454</conferenceId>
<conferencetitle>quanshi meetting</conferencetitle>
<smeAccount>http://www.jb51.net</smeAccount>
</conference>
<conference>
<conferenceId>6767</conferenceId>
<conferencetitle>quanshi meetting</conferencetitle>
<smeAccount>http://www.jb51.net</smeAccount>
</conference>
<conference>
<conferenceId>232323</conferenceId>
<conferencetitle>quanshi uuu</conferencetitle>
<smeAccount>http://www.jb51.net</smeAccount>
</conference>
<conference>
<conferenceId>8989</conferenceId>
<conferencetitle>quanshi meetting</conferencetitle>
<smeAccount>http://www.jb51.net</smeAccount>
</conference>
<conference>
<conferenceId>1234343212</conferenceId>
<conferencetitle>quanshi meetting</conferencetitle>
<smeAccount>http://www.jb51.net</smeAccount>
</conference>
</conferenceList>
*/
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
Copy Code code as follows:

/**
*
* Convert simple XML into associative arrays
* @param string $xmlString XML string
* @example
* <?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?>
<RTXConferenceReqDTO>
<conferencetitle>it Exchange </conferenceTitle>
<starttime>2011-12-19 12:00:00</starttime>
<rtxAccount>andy1111111</rtxAccount>
<ipAddr>192.168.1.56</ipAddr>
<duration>120</duration>
<conferenceType>1</conferenceType>
<invitees>
<invitee>
<rtxAccount> RTX Account Number 1 of the invited person </rtxAccount>
<tel> invited person 1 phone number </tel>
</invitee>
<invitee>
<rtxAccount> RTX Account Number 2 of the invited person </rtxAccount>
<tel> invited person 2 phone number </tel>
</invitee>
</invitees>
</RTXConferenceReqDTO>
Associative array after transformation:
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] => 1 rtx account number of the invited person
[Tel] => invited 1 phone number
)
[1] => Array
(
[Rtxaccount] => 2 rtx account number of the invited person
[Tel] => invited 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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.