I have a simple XML document containing two city id’s.
<?xml version="1.0" encoding="ISO-8859-1"?>
<config>
<city>
<id>London</id>
</city>
<city>
<id>New York</id>
</city>
</config>
When iterating over the XML I can only pick up the first city id, e.g. London.
<?php
$configFile = 'cityConfig.xml';
function getCityId($configFile) {
$xml = new SimpleXmlElement(file_get_contents("cityConfig.xml"));
$cities = array();
foreach ($xml->city->id as $cityId) {
$cityId = (string) $cityId;
array_push($cities, $cityId);
}
return $cities;
}
print_r(getCityId($configFile));
?>
<?php
The output from the above:
// Array ( [0] => London )
I’m casting $cityId into a string to be used elsewhere in my website.
Any ideas where I’m going wrong?
Thanks in advance.
Should be: