0

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.

Rizier123
  • 58,877
  • 16
  • 101
  • 156
Tani Partner
  • 117
  • 2
  • 11
  • Why do you need to convert this to an array first? Why can't you just use that XML to insert the values to your database? – Gordon Jul 29 '15 at 12:59
  • I'd tried doing that but it is creating so many problems and i've to create cron job for the same which will run everyday. So for cron job its better if i've php array instead of xml – Tani Partner Jul 29 '15 at 13:01
  • What problems did it create? For the purpose of inserting that data, iterating over that SimpleXml Graph is not much different than iterating over an array. – Gordon Jul 29 '15 at 13:02

1 Answers1

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

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

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

    }else{

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

    } 

}
Gordon
  • 312,688
  • 75
  • 539
  • 559
Amit Rana
  • 190
  • 5