This article will introduce to you two simple examples, converting php array to xml and converting xml array. I hope this article will be helpful to you.
php array to xml
The code is as follows
代码如下 |
复制代码 |
function array2xml($array, $xml = false){
if($xml === false){
$xml = new SimpleXMLElement('');
}
foreach($array as $key => $value){
if(is_array($value)){
array2xml($value, $xml->addChild($key));
}else{
$xml->addChild($key, $value);
}
}
return $xml->asXML();
}
header('Content-type: text/xml');
print array2xml($array);
|
|
Copy code
|
代码如下 |
复制代码 |
$s = <<
Basic
1
4
3
1
2
EOS;
$a = json_decode(json_encode((array) simplexml_load_string($s)),1);
print_r($a);
|
function array2xml($array, $xml = false){
If($xml === false){
$xml = new SimpleXMLElement('');
}
foreach($array as $key => $value){
If(is_array($value)){
array2xml($value, $xml->addChild($key));
}else{
$xml->addChild($key, $value);
}
Return $xml->asXML();
}
header('Content-type: text/xml');
print array2xml($array); |
php xml to array
The code is as follows
|
Copy code
$s = <<
Basic
1
4
3
1
2
EOS;
$a = json_decode(json_encode((array) simplexml_load_string($s)),1);
print_r($a);
http://www.bkjia.com/PHPjc/632670.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632670.htmlTechArticleThis article will introduce to you two simple examples, php array to xml and xml to array. I hope this The article will be helpful to all my friends. The php array to xml code is as follows Copy the code...
|
|