PHP - 数组到XML组合项目

时间:2016-06-04 22:44:24

标签: php arrays xml

我有这个PHP数组,我已成功转换为XML,每行"permissions": [ "<all_urls>" ] 的ID为Project,有些行具有相同的ID,每个ExternalProjectID都有{{1我想要的是将所有Project合并为一个ProjectFloorPlan,而不是一个ProjectFloorPlan个项目,只有一个Project与多个Project合并为一个Project基于ProjectFloorPlan

这是PHP数组:

Project

以下是转换为XML的PHP​​代码:

ExternalProjectID

这是输出XML:

[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
        )

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

应该有效:

// 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();