dswwuo1223 2016-06-04 22:44
浏览 29
已采纳

PHP - 数组到XML组合项目

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

  • 写回答

1条回答 默认 最新

  • dongtang8678 2016-06-05 01:48
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里