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 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败