如何将xml数组转换为php数组

时间:2015-07-29 12:51:22

标签: php arrays xml

我正在寻找物业网站。我的要求是导入属性信息及其图像和便利设施,并将其存储到数据库中。我有一个xml数组,我想将其转换为php数组。我有这样的xml数组: -

    SimpleXMLElement Object
    (
        [Listing] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [count] => 1
                        [Ad_Type] => Rent
                        [Unit_Type] => Office
                        [Unit_Model] => SimpleXMLElement Object
                            (
                            )

                        [Primary_View] => SimpleXMLElement Object
                            (
                            )

                        [Unit_Builtup_Area] => 7593.00
                        [No_of_Bathroom] => 2
                        [Property_Title] => Business Center /  BANKS offices/ Call Centre Offices
                        [Web_Remarks] => SimpleXMLElement Object
                            (
                            )

                        [Emirate] => Dubai
                        [Community] => Sheikh Zayed Road
                        [Property_Name] => Millennium Plaza
                        [Property_Ref_No] => AMB-R-1142
                        [Listing_Agent] => Janette Ceniza
                        [Listing_Agent_Phone] => 0564843282
                        [Listing_Date] => 2015-03-26 4:53:51 pm
                        [Last_Updated] => 2015-07-29 1:44:23 pm
                        [Bedrooms] => SimpleXMLElement Object
                            (
                            )

                        [Listing_Agent_Email] => consult1@khalidalattar.com
                        [Price] => 1200000
                        [Frequency] => per year
                        [Unit_Reference_No] => AMB-R-1142
                        [No_of_Rooms] => SimpleXMLElement Object
                            (
                            )

                        [Latitude] => 25.062252
                        [Longitude] => 55.130672
                        [unit_measure] => Sq.Ft.
                        [Featured] => 0
                        [Images] => SimpleXMLElement Object
                            (
                                [image] => Array
                                    (
                                        [0] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_19-1605-6d8e8f3aa6e53b45154eac91e2109ba4.jpg
                                        [1] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_35-1605-660a612b520978e1249c29b7fb591b45.jpg
                                        [2] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_36-1605-01d394a6a096b9a0252b30a10fd59c13.jpg
                                        [3] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_38-1605-80df60708c007c5a85721b89c97836d8.jpg
                                        [4] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_38-1605-1f9ffdde85af52dfc6f3548264c34ce0.jpg
                                        [5] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_44-1605-322badce946a4883abe602bf8c221a93.jpg
                                        [6] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_44-1605-117288432442fd54dae0e68238f39537.jpg
                                        [7] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_54-1605-914e38a1abe6418ea10885b94e4f26de.jpg
                                        [8] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_55-1605-fc756a57f7280678b41b07340e3d1e60.jpg
                                        [9] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_55-1605-53eb6e2277f1b19ee40a36660086a20e.jpg
                                        [10] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_58_36-1605-9e54faba2c9f96e31646c0660f3f0153.jpg
                                    )

                            )

                        [Facilities] => SimpleXMLElement Object
                            (
                                [facility] => Array
                                    (
                                        [0] => Built in wardrobes
                                        [1] => Covered parking
                                        [2] => Public parking
                                    )

                            )

                        [company_name] => Amber Real Estate
                        [Web_Tour] => SimpleXMLElement Object
                            (
                            )

                        [Threesixty_Tour] => SimpleXMLElement Object
                            (
                            )

                        [Audio_Tour] => SimpleXMLElement Object
                            (
                            )

                        [Virtual_Tour] => SimpleXMLElement Object
                            (
                            )

                        [QR_Code] => SimpleXMLElement Object
                            (
                            )

                        [company_logo] => http://crm.propspace.com/application/views/pictures/logos/16051408971828.png
                        [Parking] => 2
                        [Strno] => SimpleXMLElement Object
                            (
                            )

                        [PreviewLink] => http://crm.propspace.com/preview/index/14273740230306248/1605/?l_id=1576257622628120
                    )
            )
        )

有这样的多个条目。我给了参考的第一个条目。为了将这个xml转换为php数组,我编写了以下代码: -

function convert_xml($xml){
    $arr = array();
    foreach($xml->children() as $property){
        if(count($property->children()) == 0){
            $arr[$property->getName()] = strval($property);
        }else{
            $arr[$property->getName()][] = xml2array($property);
        } 
    }
    return $arr;
}
$xml_Arr = convert_xml($xml);
echo "<pre>"; print_r($xml_Arr); exit;

我得到的结果如下面给出的php数组。

Array
        (
            [Listing] => Array
                (
                    [0] => Array
                        (
                            [count] => 1
                            [Ad_Type] => Rent
                            [Unit_Type] => Office
                            [Unit_Model] => 
                            [Primary_View] => 
                            [Unit_Builtup_Area] => 7593.00
                            [No_of_Bathroom] => 2
                            [Property_Title] => Business Center /  BANKS offices/ Call Centre Offices
                            [Emirate] => Dubai
                            [Community] => Sheikh Zayed Road
                            [Property_Name] => Millennium Plaza
                            [Property_Ref_No] => AMB-R-1142
                            [Listing_Agent] => Janette Ceniza
                            [Listing_Agent_Phone] => 0564843282
                            [Listing_Date] => 2015-03-26 4:53:51 pm
                            [Last_Updated] => 2015-07-29 1:44:23 pm
                            [Bedrooms] => 
                            [Listing_Agent_Email] => consult1@khalidalattar.com
                            [Price] => 1200000
                            [Frequency] => per year
                            [Unit_Reference_No] => AMB-R-1142
                            [No_of_Rooms] => 
                            [Latitude] => 25.062252
                            [Longitude] => 55.130672
                            [unit_measure] => Sq.Ft.
                            [Featured] => 0
                            [Images] => Array
                                (
                                    [0] => Array
                                        (
                                            [image] => http://crm.propspace.com/watermark?c_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_58_36-1605-9e54faba2c9f96e31646c0660f3f0153.jpg
                                        )

                                )

                            [Facilities] => Array
                                (
                                    [0] => Array
                                        (
                                            [facility] => Public parking
                                        )

                                )

                            [company_name] => Amber Real Estate
                            [Web_Tour] => 
                            [Threesixty_Tour] => 
                            [Audio_Tour] => 
                            [Virtual_Tour] => 
                            [QR_Code] => 
                            [company_logo] => http://crm.propspace.com/application/views/pictures/logos/16051408971828.png
                            [Parking] => 2
                            [Strno] => 
                            [PreviewLink] => http://crm.propspace.com/preview/index/14273740230306248/1605/?l_id=1576257622628120
                        )
                        )
                        )

图像数组中有10个元素,但它只返回最后一个图像。在我的结果中我也想要图像数组。

1 个答案:

答案 0 :(得分:-1)

foreach($xml->children() as $key=>$property){

    if(count($property->children()) == 0){

        $arr[$property->getName()] = strval($property);

    }else{

        $arr[$property->getName()][$key] = xml2array($property);

    } 

}