우리는 array_walk_recursive() function.array_walk_recursive()를 사용하여 위의 문제를 해결할 수 있습니다.array_walk_recursive()는 내장 PHP 함수입니다. 이 함수는 배열의 키를 값으로 변환하고 배열의 값을 XML의 요소로 변환하는 XML 문서로 배열을 변환합니다.
간단한 예를 들어 설명하겠습니다.
예시
<?php
$array = array (
'name' => 'alex',
'empdept' => 'account',
'address' => array (
'city' => 'michigan'
),
);
//This function create a xml object with element root.
$xml = new SimpleXMLElement('');
array_walk_recursive($array, array ($xml,'addChild'));
print $xml->asXML();
?> 인쇄 출력
<?xml version="1.0"?> <root> <name> alex </name> <empdept> account </empdept> <city> michigan </city > </root>
참고
PHP Fatal error:Uncaught Error:Class 'SimpleXMLElement' not found in 다음과 같은 오류 메시지가 표시되면 php-xml, php-simplexml 패키지를 설치하기만 하면 됩니다.