Home >Backend Development >PHP Tutorial >PHP array to xml and xml conversion array example_PHP tutorial

PHP array to xml and xml conversion array example_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 10:49:59802browse

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.html
www.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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn