如何将XML数组转换为PHP数组 [英] how to convert xml array to php array

查看:106
本文介绍了如何将XML数组转换为PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作性质的网站。我的要求是进口财产信息与图像和设施,并存储在数据库。我有一个xml数组,我想将其转换成PHP数组。我有XML数组是这样的: -

  SimpleXMLElement对象
    (
        [上市] =>排列
            (
                [0] => SimpleXMLElement对象
                    (
                        [计] => 1
                        [AD_TYPE] =>出租
                        [UNIT_TYPE] =>办公室
                        [Unit_Model] => SimpleXMLElement对象
                            (
                            )                        [Primary_View] => SimpleXMLElement对象
                            (
                            )                        [Unit_Builtup_Area] => 7593.00
                        [No_of_Bathroom] => 2
                        [Property_Title] =>商务中心/办事处银行/呼叫中心办公室
                        [Web_Remarks] => SimpleXMLElement对象
                            (
                            )                        [阿联酋] =>迪拜
                        [社区] => Sheikh Zayed路
                        [PROPERTY_NAME] =>千禧广场
                        [Property_Ref_No] => AMB-R-1142
                        [Listing_Agent] =>珍妮特Ceniza
                        [Listing_Agent_Phone] => 0564843282
                        [Listing_Date] => 2015年3月26日下午4时53分51秒
                        [LAST_UPDATED] => 2015年7月29日下午1时44分23秒
                        [卧室] => SimpleXMLElement对象
                            (
                            )                        [Listing_Agent_Email] => consult1@khalidalattar.com
                        [参考价格] => 120
                        [频率] =>每年
                        [Unit_Reference_No] => AMB-R-1142
                        [No_of_Rooms] => SimpleXMLElement对象
                            (
                            )                        [纬度] => 25.062252
                        [经度] => 55.130672
                        [unit_measure] =>平方呎
                        [精选] => 0
                        [图片] => SimpleXMLElement对象
                            (
                                [图片] =>排列
                                    (
                                        [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
                                    )                            )                        [设施] => SimpleXMLElement对象
                            (
                                [设施] =>排列
                                    (
                                        [0] =>内置衣柜
                                        [1] =>室内停车场
                                        [2] =>公共停车场
                                    )                            )                        [COMPANY_NAME] =>琥珀房地产
                        [Web_Tour] => SimpleXMLElement对象
                            (
                            )                        [Threesixty_Tour] => SimpleXMLElement对象
                            (
                            )                        [Audio_Tour] => SimpleXMLElement对象
                            (
                            )                        [Virtual_Tour] => SimpleXMLElement对象
                            (
                            )                        [QR_ code => SimpleXMLElement对象
                            (
                            )                        [company_logo] => http://crm.propspace.com/application/views/pictures/logos/16051408971828.png
                        [停车场] => 2
                        [Strno] => SimpleXMLElement对象
                            (
                            )                        [previewLink] => http://crm.propspace.com/$p$pview/index/14273740230306248/1605/?l_id=1576257622628120
                    )
            )
        )

有这样的多个条目。我已经给出了参鉴第一个条目。对于这个XML转换为PHP数组我写了下面code: -

 函数convert_xml($ XML){
    $ ARR =阵列();
    的foreach($ XML的>儿童()为$属性){
        如果(计数($财产而>儿童())== 0){
            $改编[$财产而>的getName()] = strval($属性);
        }其他{
            $常用3 [$财产而>的getName()] [] = xml2array($属性);
        }
    }
    返回$ ARR;
}
$ xml_Arr = convert_xml($ XML);
回声< pre>中;的print_r($ xml_Arr);出口;

我m到处下面给出的结果作为PHP数组。

 阵列
        (
            [上市] =>排列
                (
                    [0] =>排列
                        (
                            [计] => 1
                            [AD_TYPE] =>出租
                            [UNIT_TYPE] =>办公室
                            [Unit_Model] =>
                            [Primary_View] =>
                            [Unit_Builtup_Area] => 7593.00
                            [No_of_Bathroom] => 2
                            [Property_Title] =>商务中心/办事处银行/呼叫中心办公室
                            [阿联酋] =>迪拜
                            [社区] => Sheikh Zayed路
                            [PROPERTY_NAME] =>千禧广场
                            [Property_Ref_No] => AMB-R-1142
                            [Listing_Agent] =>珍妮特Ceniza
                            [Listing_Agent_Phone] => 0564843282
                            [Listing_Date] => 2015年3月26日下午4时53分51秒
                            [LAST_UPDATED] => 2015年7月29日下午1时44分23秒
                            [卧室] =>
                            [Listing_Agent_Email] => consult1@khalidalattar.com
                            [参考价格] => 120
                            [频率] =>每年
                            [Unit_Reference_No] => AMB-R-1142
                            [No_of_Rooms] =>
                            [纬度] => 25.062252
                            [经度] => 55.130672
                            [unit_measure] =>平方呎
                            [精选] => 0
                            [图片] =>排列
                                (
                                    [0] =>排列
                                        (
                                            [图片] => 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
                                        )                                )                            [设施] =>排列
                                (
                                    [0] =>排列
                                        (
                                            [设施] =>公共停车场
                                        )                                )                            [COMPANY_NAME] =>琥珀房地产
                            [Web_Tour] =>
                            [Threesixty_Tour] =>
                            [Audio_Tour] =>
                            [Virtual_Tour] =>
                            [QR_ code =>
                            [company_logo] => http://crm.propspace.com/application/views/pictures/logos/16051408971828.png
                            [停车场] => 2
                            [Strno] =>
                            [previewLink] => http://crm.propspace.com/$p$pview/index/14273740230306248/1605/?l_id=1576257622628120
                        )
                        )
                        )

有图像阵列10内的元素,但它返回只有最后的图像。我想图像阵列,以及在我的结果。


解决方案

 的foreach($ XML的>儿童()为$关键=> $属性){    如果(计数($财产而>儿童())== 0){        $改编[$财产而>的getName()] = strval($属性);    }其他{        $改编[$财产而>的getName()] [$关键] = xml2array($属性);    }}

I am working on property site. My requirement is to import property info with its images and amenities and store it in to database. I've an xml array and i want to convert it into php array. I've xml array like this:-

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

There are multiple entries like this. I'd given first entry for the referrence. For converting this xml to php array I've written the below code:-

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;

I m getting the result as php array given below.

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

There are 10 element inside image array, but it is returning only the last image. I want image array as well in my result.

解决方案

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

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

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

    }else{

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

    } 

}

这篇关于如何将XML数组转换为PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆