php数组转换成xml的方法:首先创建一个PHP示例文件;然后定义一个“array_Xml”方法;接着通过foreach循环遍历以及“is_array”等函数实现转换逻辑;最后执行该文件即可。
PHP实现数组array转换成xml的方法,具体如下:
<?php $elementLevel = 0 ; function array_Xml($array, $keys = '') { global $elementLevel; if(!is_array($array)) { if($keys == ''){ return $array; }else{ return "\n<$keys>" . $array . "</$keys>\n"; } }else{ foreach ($array as $key => $value) { $haveTag = true; if (is_numeric($key)) { $key = $keys; $haveTag = false; } if($elementLevel == 0 ) { $startElement = "<$key>"; $endElement = "</$key>"; } $text .= $startElement; if(!$haveTag) { $elementLevel++; $text .= "<$key>" . array_Xml($value, $key). "</$key>\n"; }else{ $elementLevel++; $text .= array_Xml($value, $key); } $text .= $endElement; } } return $text; } $array = array( "employees" => array( "employee" => array( array( "name" => "name one", "position" => "position one" ), array( "name" => "name two", "position" => "position two" ), array( "name" => "name three", "position" => "position three" ) ) ) ); echo array_Xml($array); ?>
推荐:《PHP教程》
以上是php数组转换成xml格式的实现方法的详细内容。更多信息请关注PHP中文网其他相关文章!