【发布时间】:2016-10-03 17:44:47
【问题描述】:
我有一个这样的数组:
[0] => Array
(
[Project] => Array
(
[ExternalProjectID] => 53
[ProjectName] => Doon Creek
[Location] => Array
(
[Address] => 123 Fake Street
[City] => Toronto
[Province] => ON
[Latitude] => 43.0000
[Longitude] => -80.0000
)
[Website] => http://www.website.com/our-communities.php?newcommunity=53
[ContactInformation] => Array
(
[ContactPhone] => 555-5555
[ContactEmail] => email@email.com
[SalesOfficeAddress] => 123 Fake Street
[SalesOfficeCity] => Toronto
[SalesOfficeProvince] => ON
)
)
)
现在我试图将它转换为 XML 究竟它在数组中的样子,比如 Project 应该是一个包含所有内容的节点,Location 也应该是一个节点,其中包含该节点内的 Location 数组中的所有内容,以及 ContactInformation。
这就是我所拥有的:
$xml = new SimpleXMLElement();
$node = $xml->addChild('Projects');
array_to_xml($newArray, $node);
echo $xml->asXML();
die();
function array_to_xml($array, &$xml) {
foreach($array as $key => $value) {
if(is_array($value)) {
if(!is_numeric($key)){
$subnode = $xml->addChild("$key");
array_to_xml($value, $xml);
} else {
array_to_xml($value, $xml);
}
} else {
$xml->addChild("$key","$value");
}
}
}
但 Project、Location 和 ContactInformation 返回如下:
<Projects>
<Project />
<ExternalProjectID>53</ExternalProjectID>
<ProjectName>Doon Creek</ProjectName>
<Location />
<Address>123 Fake St.</Address>
<City>Toronto</City>
<Province>ON</Province>
<Latitude>43.0000</Latitude>
<Longitude>-80.000</Longitude>
<Website>http://www.website.com/our-communities.php?newcommunity=53</Website>
<ContactInformation />
<ContactPhone>555-5555</ContactPhone>
<ContactEmail>email@email.com</ContactEmail>
<SalesOfficeAddress>123 Fake Street</SalesOfficeAddress>
<SalesOfficeCity>Toronto</SalesOfficeCity>
<SalesOfficeProvince>ON</SalesOfficeProvince>
<Project />
</Projects>
我的问题是如何修复我的 XML 输出?
【问题讨论】:
-
你推荐this了吗?
-
第二个答案对我有用。