Bu Ángel López cevap üzerine kuruludur. Özellikleri için destek eklendi. Bir eleman nitelikleri varsa, @ ile önek ve anahtar olarak boş bir dize ile gerçek eleman içeriğine bakın.
/**
* Build an XML Data Set
*
* @param array $data Associative Array containing values to be parsed into an XML Data Set(s)
* @param string $startElement Root Opening Tag, default fx_request
* @param string $xml_version XML Version, default 1.0
* @param string $xml_encoding XML Encoding, default UTF-8
* @return string XML String containig values
* @return mixed Boolean false on failure, string XML result on success
*/
function arrayToXML($data, $startElement = 'fx_request', $xml_version = '1.0', $xml_encoding = 'UTF-8'){
if(!is_array($data)){
$err = 'Invalid variable type supplied, expected array not found on line '.__LINE__." in Class: ".__CLASS__." Method: ".__METHOD__;
trigger_error($err);
//if($this->_debug) echo $err;
return false; //return false error occurred
}
$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument($xml_version, $xml_encoding);
$xml->startElement($startElement);
/**
* Write keys in $data prefixed with @ as XML attributes, if $data is an array. When an @ prefixed key is found, a '' key is expected to indicate the element itself.
* @param object $xml XMLWriter Object
* @param array $data with attributes filtered out
*/
function writeAttr(XMLWriter $xml, $data) {
if(is_array($data)) {
$nonAttributes = array();
foreach($data as $key => $val) {
//handle an attribute with elements
if($key[0] == '@') {
$xml->writeAttribute(substr($key, 1), $val);
} else if($key == '') {
if(is_array($val)) $nonAttributes = $val;
else $xml->text("$val");
}
//ignore normal elements
else $nonAttributes[$key] = $val;
}
return $nonAttributes;
}
else return $data;
}
/**
* Write XML as per Associative Array
* @param object $xml XMLWriter Object
* @param array $data Associative Data Array
*/
function writeEl(XMLWriter $xml, $data) {
foreach($data as $key => $value) {
if(is_array($value) && isset($value[0])) { //numeric array
foreach($value as $itemValue){
if(is_array($itemValue)) {
$xml->startElement($key);
$itemValue = writeAttr($xml, $itemValue);
writeEl($xml, $itemValue);
$xml->endElement();
} else {
$itemValue = writeAttr($xml, $itemValue);
$xml->writeElement($key, "$itemValue");
}
}
} else if(is_array($value)) { //associative array
$xml->startElement($key);
$value = writeAttr($xml, $value);
writeEl($xml, $value);
$xml->endElement();
} else { //scalar
$value = writeAttr($xml, $value);
$xml->writeElement($key, "$value");
}
}
}
writeEl($xml, $data);
$xml->endElement();//write end element
//returns the XML results
return $xml->outputMemory(true);
}
çok yo bu dönüştürebilirsiniz:
$mArray["invitations"]["user"][0]["@name"] = "paco";
$mArray["invitations"]["user"][0][""]["amigos"][0] = 82;
$mArray["invitations"]["user"][0][""]["amigos"][1] = 29;
$mArray["invitations"]["user"][0][""]["amigos"][2] = 6;
$mArray["invitations"]["user"][1]["@name"] = "jose";
$mArray["invitations"]["user"][1][""]["amigos"][0] = 43;
$mArray["invitations"]["user"][1][""]["amigos"][1]["tuyos"] = 32;
$mArray["invitations"]["user"][1][""]["amigos"][1]["mios"] = 79;
$mArray["invitations"]["user"][1][""]["amigos"][2] = 11;
$mArray["invitations"]["user"][2]["@name"] = "luis";
$mArray["invitations"]["user"][2][""]["amigos"][0] = 65;
Bu xml içine:
<invitations>
<user name="paco">
<amigos>82</amigos>
<amigos>29</amigos>
<amigos>6</amigos>
</user>
<user name="jose">
<amigos>43</amigos>
<amigos>
<tuyos>32</tuyos>
<mios>79</mios>
</amigos>
<amigos>11</amigos>
</user>
<user name="luis">
<amigos>65</amigos>
</user>
</invitations>
Teşekkürler Ángel.