0

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 protected]
            [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 protected]
            [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 protected]</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 protected]</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

0

1 Answer 1

1

It should work:

// sort projects by ExternalProjectID
usort($communitiesArray, function($first, $second) {
    $firstProjectID = $first['ExternalProjectID'];
    $secondProjectID = $second['ExternalProjectID'];

    return $firstProjectID - $secondProjectID;
});

function processProject($project, &$result) {
    $locationKeys = array('Address', 'City', 'Province', 'Latitude', 'Longitude');
    $contactKeys = array('ContactPhone', 'ContactEmail', 'SalesOfficeAddress', 'SalesOfficeCity', 'SalesOfficeProvince');
    $projectFloorPlans = array('ExternalProjectFloorPlanID', 'FloorPlanName', 'Beds', 'Baths', 'InteriorSqFtRange', 'Price');

    $newProjectID = $project['ExternalProjectID'];

    // compare current project with a previous one
    // if the previous has the same projectId, add projectFloorPlan to the previous
    // if the previous has different projectId, create new object
    if (empty($result) ||
        $result[sizeof($result) - 1]['ExternalProjectID'] !== $newProjectID) {

        $newProject = array('ProjectFloorPlans' => array());

        foreach($project as $key => $value) {
            if (in_array($key, $locationKeys)) {
                $newProject['Location'][$key] = $value;
            }
            else if (in_array($key, $contactKeys)) {
                $newProject['ContactInformation'][$key] = $value;
            }
            else if (!in_array($key, $projectFloorPlans)) {
                $newProject[$key] = $value;
            }
        }

        $result[] = &$newProject;
    }
    else {
        $newProject = &$result[sizeof($result) - 1];
    }

    $projectFloorPlan = array();
    foreach(array_intersect(array_keys($project), $projectFloorPlans) as $key) {
        $projectFloorPlan[$key] = $project[$key];
    }

    if (!empty($projectFloorPlan)) {
        $newProject['ProjectFloorPlans'][] = $projectFloorPlan;
    }

}

$newArray = array();
foreach($communitiesArray as $project) {
    processProject($project, $newArray);
}

$xml_data = new SimpleXMLElement('<Projects/>');
function array_to_xml( $data, &$xml_data) {
    foreach( $data as $key => $value ) {
        if( is_array($value) ) {
            if( is_numeric($key) ){
                $key = $xml_data->getName() === 'Projects' ? 'Project' : 'ProjectFloorPlan'; //dealing with <0/>..<n/> issues
            }
            $subnode = $xml_data->addChild($key);
            array_to_xml($value, $subnode);
        } else {
            $xml_data->addChild($key,htmlspecialchars($value));
        }
    }
}

array_to_xml($newArray,$xml_data);

echo $xml_data->asXML();
Sign up to request clarification or add additional context in comments.

2 Comments

I think there is an issue with this line: $key = $level === 0 ? 'Project' : 'ProjectFloorPlan'; //dealing with <0/>..<n/> issues Its very random sometimes I get Project when its ProjectFloorPlan and ProjectFloorPlan when its Project
@user979331 Yes, you're right, It's a bug. I've updated code in the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.