Я работаю на сайте недвижимости. Мое требование – импортировать информацию о недвижимости с ее изображениями и удобствами и хранить их в базе данных. У меня есть 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://img.ruphp.com/php/watermarkc_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_19-1605-6d8e8f3aa6e53b45154eac91e2109ba4.jpg [1] => http://img.ruphp.com/php/watermarkc_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_35-1605-660a612b520978e1249c29b7fb591b45.jpg [2] => http://img.ruphp.com/php/watermarkc_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_36-1605-01d394a6a096b9a0252b30a10fd59c13.jpg [3] => http://img.ruphp.com/php/watermarkc_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_38-1605-80df60708c007c5a85721b89c97836d8.jpg [4] => http://img.ruphp.com/php/watermarkc_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_38-1605-1f9ffdde85af52dfc6f3548264c34ce0.jpg [5] => http://img.ruphp.com/php/watermarkc_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_44-1605-322badce946a4883abe602bf8c221a93.jpg [6] => http://img.ruphp.com/php/watermarkc_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_44-1605-117288432442fd54dae0e68238f39537.jpg [7] => http://img.ruphp.com/php/watermarkc_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_54-1605-914e38a1abe6418ea10885b94e4f26de.jpg [8] => http://img.ruphp.com/php/watermarkc_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_55-1605-fc756a57f7280678b41b07340e3d1e60.jpg [9] => http://img.ruphp.com/php/watermarkc_id=1605&l_id=1576257622628120&aid=1447092&id=14273740230306248&image=26_03_2015-16_54_55-1605-53eb6e2277f1b19ee40a36660086a20e.jpg [10] => http://img.ruphp.com/php/watermarkc_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://img.ruphp.com/php/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://img.ruphp.com/php/watermarkc_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://img.ruphp.com/php/16051408971828.png [Parking] => 2 [Strno] => [PreviewLink] => http://crm.propspace.com/preview/index/14273740230306248/1605/?l_id=1576257622628120 ) ) )
Есть 10 элементов внутри массива изображений, но он возвращает только последнее изображение. Я хочу, чтобы в моем результате также был массив изображений.