I have this PHP array and I have successfully converted in XML and each row Project
has an ID ExternalProjectID
and some rows have the same ID and each Project
has a ProjectFloorPlan
what I am trying to is combine all ProjectFloorPlan
into one Project
and not have a double Project
items, just one Project
with multiple ProjectFloorPlan
into one Project
based on the ExternalProjectID
Here is the PHP Array:
[0] => Array
(
[ExternalProjectID] => 66
[ProjectName] => Astoria
[Address] => 123 Fake Street
[City] => Toronto
[Province] => ON
[Latitude] => 43.0000
[Longitude] => -79.0000
[Website] => http://www.website.com/our-communities.php?newcommunity=66
[ContactPhone] => 555-5555
[ContactEmail] => email@email.com
[SalesOfficeAddress] => 123 Fake Street
[SalesOfficeCity] => Toronto
[SalesOfficeProvince] => ON
[ExternalProjectFloorPlanID] => 2036
[FloorPlanName] => The Paisley
[Beds] => 3
[Baths] => 2.5
[InteriorSqFtRange] => 1784
[Price] => 481900
)
[1] => Array
(
[ExternalProjectID] => 66
[ProjectName] => Astoria
[Address] => 123 Fake Street
[City] => Toronto
[Province] => ON
[Latitude] => 43.0000
[Longitude] => -79.0000
[Website] => http://www.website.com/our-communities.php?newcommunity=66
[ContactPhone] => 555-5555
[ContactEmail] => email@email.com
[SalesOfficeAddress] => 123 Fake Street
[SalesOfficeCity] => Toronto
[SalesOfficeProvince] => ON
[ExternalProjectFloorPlanID] => 2037
[FloorPlanName] => The Chino
[Beds] => 3
[Baths] => 2.5
[InteriorSqFtRange] => 1698
[Price] => 472900
)
Here is the PHP code to convert to XML:
$newArray = array();
$locationKeys = array('Address', 'City', 'Province', 'Latitude', 'Longitude');
$contactKeys = array('ContactPhone', 'ContactEmail', 'SalesOfficeAddress', 'SalesOfficeCity', 'SalesOfficeProvince');
$projectFloorPlans = array('ExternalProjectFloorPlanID', 'FloorPlanName', 'Beds', 'Baths', 'InteriorSqFtRange', 'Price');
foreach($communitiesArray as $projects) {
foreach($projects as $key => $value) {
if(in_array($key, $locationKeys)) {
$project['Location'][$key] = $value;
} else if(in_array($key, $contactKeys)) {
$project['ContactInformation'][$key] = $value;
} else if(in_array($key, $projectFloorPlans)) {
$project['ProjectFloorPlans']['ProjectFloorPlan'][$key] = $value;
} else {
$project[$key] = $value;
}
}
$newArray[] = $project;
}
$xml_data = new SimpleXMLElement();
function array_to_xml( $data, &$xml_data ) {
foreach( $data as $key => $value ) {
if( is_array($value) ) {
if( is_numeric($key) ){
$key = 'Project'; //dealing with <0/>..<n/> issues
}
$subnode = $xml_data->addChild($key);
array_to_xml($value, $subnode);
} else {
$xml_data->addChild("$key",htmlspecialchars("$value"));
}
}
}
$node = $xml_data->addChild('Projects');
array_to_xml($newArray,$node);
echo $xml_data->asXML();
and here is the output XML:
<Project>
<ExternalProjectID>66</ExternalProjectID>
<ProjectName>Astoria</ProjectName>
<Location>
<Address>123 Fake Street</Address>
<City>Toronto</City>
<Province>ON</Province>
<Latitude>43.0000</Latitude>
<Longitude>-79.0000</Longitude>
</Location>
<Website>http://www.website.com/our-communities.php?newcommunity=66</Website>
<ContactInformation>
<ContactPhone>555-5555</ContactPhone>
<ContactEmail>email@email.com</ContactEmail>
<SalesOfficeAddress>123 Fake Street</SalesOfficeAddress>
<SalesOfficeCity>Toronto</SalesOfficeCity>
<SalesOfficeProvince>ON</SalesOfficeProvince>
</ContactInformation>
<ProjectFloorPlans>
<ProjectFloorPlan>
<ExternalProjectFloorPlanID>2036</ExternalProjectFloorPlanID>
<FloorPlanName>The Paisley</FloorPlanName>
<Beds>3</Beds>
<Baths>2.5</Baths>
<InteriorSqFtRange>1784</InteriorSqFtRange>
<Price>481900</Price>
</ProjectFloorPlan>
</ProjectFloorPlans>
</Project>
<Project>
<ExternalProjectID>66</ExternalProjectID>
<ProjectName>Astoria</ProjectName>
<Location>
<Address>123 Fake Street</Address>
<City>Toronto</City>
<Province>ON</Province>
<Latitude>43.0000</Latitude>
<Longitude>-79.0000</Longitude>
</Location>
<Website>http://www.website.com/our-communities.php?newcommunity=66</Website>
<ContactInformation>
<ContactPhone>555-5555</ContactPhone>
<ContactEmail>email@email.com</ContactEmail>
<SalesOfficeAddress>123 Fake Street</SalesOfficeAddress>
<SalesOfficeCity>Toronto</SalesOfficeCity>
<SalesOfficeProvince>ON</SalesOfficeProvince>
</ContactInformation>
<ProjectFloorPlans>
<ProjectFloorPlan>
<ExternalProjectFloorPlanID>2037</ExternalProjectFloorPlanID>
<FloorPlanName>The Chino</FloorPlanName>
<Beds>3</Beds>
<Baths>2.5</Baths>
<InteriorSqFtRange>1698</InteriorSqFtRange>
<Price>472900</Price>
</ProjectFloorPlan>
</ProjectFloorPlans>
</Project>
Any help would be appreciated