I need to get some data from a xml output:
[current_condition] => SimpleXMLElement Object
(
[observation_time] => 12:22 PM
[temp_C] => 18
[temp_F] => 64
[weatherCode] => 116
[weatherIconUrl] => SimpleXMLElement Object
(
)
[weatherDesc] => SimpleXMLElement Object
(
)
[windspeedMiles] => 4
[windspeedKmph] => 7
[winddirDegree] => 180
[winddir16Point] => S
[precipMM] => 0.1
[humidity] => 52
[visibility] => 10
[pressure] => 1023
[cloudcover] => 50
)
[weather] => Array
(
[0] => SimpleXMLElement Object
(
[date] => 2013-04-23
[tempMaxC] => 20
[tempMaxF] => 69
[tempMinC] => 7
[tempMinF] => 44
[windspeedMiles] => 5
[windspeedKmph] => 8
[winddirection] => SSW
[winddir16Point] => SSW
[winddirDegree] => 210
[weatherCode] => 113
[weatherIconUrl] => SimpleXMLElement Object
(
)
[weatherDesc] => SimpleXMLElement Object
(
)
[precipMM] => 0.7
)
[1] => SimpleXMLElement Object
(
[date] => 2013-04-24
[tempMaxC] => 25
[tempMaxF] => 76
[tempMinC] => 8
[tempMinF] => 46
[windspeedMiles] => 3
[windspeedKmph] => 5
[winddirection] => NNE
[winddir16Point] => NNE
[winddirDegree] => 24
[weatherCode] => 113
[weatherIconUrl] => SimpleXMLElement Object
(
)
[weatherDesc] => SimpleXMLElement Object
(
)
[precipMM] => 0.3
)
)
Im using
printf("<p>Current temperature %s and code %s</p>",
$xml->current_condition->temp_C, $xml->current_condition->weatherCode);
to output the temperature and the weather code, and is working fine, is outputing:
Current temperature 18 and code 116
Now how can I TempMaxC and TempMinC from Weather Array [0]
and also from array [1]
Thanks
You can use a foreach:
foreach($xml->weather as $weatherObj) {
echo "<h2>Weather on {$weatherObj->date}</h2>";
echo "<ul>";
echo "<li>Max temp: {$weatherObj->tempMaxC}</li>";
echo "<li>Min temp: {$weatherObj->tempMinC}</li>";
echo "</ul>";
}
Related
How to get "file_url" from this data?
Response can contain few objects, and how to parse it?
Code that I use:
<?php
$r34data = simplexml_load_file('https://rule34.xxx/index.php?page=dapi&s=post&q=index&tags=green_hair&limit=2');
print_r($r34data);
Response:
SimpleXMLElement Object ( [#attributes] => Array ( [count] => 69175 [offset] => 0 ) [post] => Array ( [0] => SimpleXMLElement Object ( [#attributes] => Array ( [height] => 2039 [score] => 1 [file_url] => https://wimg.rule34.xxx/images/3820/01e0a587f6e303c34669a4f79c994100.png [parent_id] => [sample_url] => https://rule34.xxx/samples/3820/sample_01e0a587f6e303c34669a4f79c994100.jpg [sample_width] => 850 [sample_height] => 667 [preview_url] => https://rule34.xxx/thumbnails/3820/thumbnail_01e0a587f6e303c34669a4f79c994100.jpg [rating] => e [tags] => 1boy 2girls annette_(fire_emblem) annette_fantine_dominic ass back back_view bare_back big_ass big_breasts blue_eyes blue_lingerie breasts_outside byleth_(fire_emblem) byleth_(fire_emblem)_(male) byleth_(male) ffm_threesome fingering fingering_partner fire_emblem fire_emblem:_three_houses french_kiss green_hair kissing light_blue_lingerie lingerie long_hair lysithea_(fire_emblem) lysithea_von_ordelia medium_breasts medium_hair nintendo nipples orange_hair pink_eyes purple_lingerie pussy pussy_juice pussy_juice_trail quan_ming sex threesome vaginal_penetration white_hair [id] => 4324333 [width] => 2600 [change] => 1608630074 [md5] => 01e0a587f6e303c34669a4f79c994100 [creator_id] => 503169 [has_children] => false [created_at] => Tue Dec 22 09:21:59 +0000 2020 [status] => active [source] => https://www.pixiv.net/en/artworks/86455582 [has_notes] => false [has_comments] => false [preview_width] => 150 [preview_height] => 117 ) ) [1] => SimpleXMLElement Object ( [#attributes] => Array ( [height] => 1600 [score] => 10 [file_url] => https://wimg.rule34.xxx/images/3820/4a88c87d1cb18928bcee85207bf1c61b.jpeg [parent_id] => [sample_url] => https://rule34.xxx/samples/3820/sample_4a88c87d1cb18928bcee85207bf1c61b.jpg [sample_width] => 850 [sample_height] => 618 [preview_url] => https://rule34.xxx/thumbnails/3820/thumbnail_4a88c87d1cb18928bcee85207bf1c61b.jpg [rating] => e [tags] => 1boy 1boy1girl 1girls all_fours anal anal_sex barefoot bent_over big_breasts christmas clothed_male_nude_female clothing cum cum_in_ass cum_inside cum_leaking doggy_style ejaculation ejaculation_while_penetrated faceless_male feet female female_focus forehead_protector green_hair hair_bun headband huge_breasts kneeling long_hair looking_at_viewer looking_back male multicolored_hair naked naruto naruto_(series) naruto_shippuden nude open_mouth orange_eyes orange_hair pakura penetration pussy santa_costume smile soles standing tied_hair toes two_tone_hair vagina [id] => 4324142 [width] => 2202 [change] => 1608622103 [md5] => 4a88c87d1cb18928bcee85207bf1c61b [creator_id] => 631992 [has_children] => false [created_at] => Tue Dec 22 07:28:01 +0000 2020 [status] => active [source] => [has_notes] => false [has_comments] => false [preview_width] => 150 [preview_height] => 108 ) ) ) )
Please help!
(don't ask why I parsing rule34)
Hi You can conver it to the array and work with is easy
libxml_use_internal_errors(TRUE);
$objXmlDocument = simplexml_load_file("https://rule34.xxx/index.php?page=dapi&s=post&q=index&tags=green_hair&limit=2");
if ($objXmlDocument === FALSE) {
echo "There were errors parsing the XML file.\n";
foreach(libxml_get_errors() as $error) {
echo $error->message;
}
exit;
}
$objJsonDocument = json_encode($objXmlDocument);
$arrOutput = json_decode($objJsonDocument, TRUE);
echo "<pre>";
print_r($arrOutput);
I am trying to read XML attributes from XML file. I want to echo baadNavn.
<?php
$url = "https://xml.websejler.dk/da/baad/1234";
$xml = simplexml_load_file($url);
print_r($xml);
echo $xml[baad][baadNavn];
?>
What do I wrong?
Print_r gives me:
SimpleXMLElement Object ( [baad] => SimpleXMLElement Object ( [#attributes] => Array ( [baadId] => 1234 [maalerId] => 32217 [baadNavn] => Ongo Bongo [baadStatus] => dhkolbaad [dhKlasseStatus] => skrogmedspeciel [nation] => DEN [sejlnummer] => 46 [byggerNavn] => Granada Yacht [byggeAar] => 1978 [maaleDato] => 1991-05-13T00:00:00 [stabgodk] => 0 [skrogMaterialeId] => 1003 [materiale] => GRP [opfolg] => 0 [fysisk] => 1 [baadtypeId] => 146 [rigsejlId] => 28158 [skrogId] => 17214 [specielId] => 79368 ) [certifikat] => SimpleXMLElement Object ( [#attributes] => Array ( [certifikatId] => 1234 [gyldigt] => 0 ) ) [baadtype] => SimpleXMLElement Object ( [#attributes] => Array ( [baadtypeId] => 146 [baadtypeNavn] => GRANADA 31 [baadStatus] => dhkolbaad [dhKlasseStatus] => skrogmedspeciel [dhBeregnes] => 1 [bem] => Granada 311 har samme skrog som Granada 31. De første ca. 35 var med blykøl (KC 0,96) - senere kommer det med jernkøl (KC 1,17) [skrogId] => 17214 [specielId] => 79368 ) ) [rigsejl] => SimpleXMLElement Object ( [#attributes] => Array ( [RigsejlId] => 28158 [E] => 3.27 [P] => 11.14 [HB] => 0.15 [MGM] => 2.10 [MGU] => 1.21 [Tmax] => 12.10 [LP] => 5.23 [FSP] => 0.08 [SPL] => 0.00 [J] => 3.49 [TPS] => 0.00 [JHW] => 0.00 [ISP] => 12.38 [SL] => 0.00 [SLU] => 0.00 [SLE] => 0.00 [SF] => 0.00 [SMG] => 0.00 [SFA] => 0.00 [SMGA] => 0.00 [PropelId] => 2 [propel] => Fri foldepropel med 2-3 blade (2) [RF] => 0 [MF] => 0 [HF] => 0 ) ) [skrog] => SimpleXMLElement Object ( [#attributes] => Array ( [SkrogId] => 17214 [Gmax] => 6.42 [SGmax] => 5.59 [FBSB] => 0.93 [FBBB] => 0.94 [SBmax] => 5.73 [UDFSB] => 0.26 [UDFBB] => 0.26 [OF] => 1.17 [OA] => 0.00 [UDHBmax] => 0.00 [UDHmax] => 0.00 [STF] => 1.11 [AF] => 0.00 ) ) [speciel] => SimpleXMLElement Object ( [#attributes] => Array ( [SpecielId] => 79368 [Bmax] => 3.05 [LOA] => 9.38 [D] => 4.17 [K] => 1.80 [KC] => 0.96 [KFId] => 1 [KF] => Normal [KontrolVejet] => 0 [KontrolMaalt] => 0 [KontrolKrenget] => 0 ) ) [ejer] => SimpleXMLElement Object ( [#attributes] => Array ( [personId] => 53371 [klubId] => 318 [klubNavn] => Aarhus Sejlklub [arrangor] => 1 ) ) [offentligmaaler] => SimpleXMLElement Object ( [#attributes] => Array ( [personId] => 32217 [aktiv] => 0 [maaHentePersonlister] => 0 [offentlig] => 0 [sejlmager] => 0 [maalerbem] => [navn] => Niels Agerskov ) ) [beregning] => SimpleXMLElement Object ( [#attributes] => Array ( [baadId] => 1234 [status] => beregnet [oprettetTid] => 2019-05-10T12:15:32.637 [beregnetTid] => 2019-05-10T12:16:31.447 [beregnetOATid] => 43595.5115 [beskedSendes] => 1 ) ) ) )
XML files contains:
<websejler>
<baad baadId="1234" maalerId="32217" baadNavn="Ongo Bongo" baadStatus="dhkolbaad" dhKlasseStatus="skrogmedspeciel" nation="DEN" sejlnummer="46" byggerNavn="Granada Yacht" byggeAar="1978" maaleDato="1991-05-13T00:00:00" stabgodk="0" skrogMaterialeId="1003" materiale="GRP" opfolg="0" fysisk="1" baadtypeId="146" rigsejlId="28158" skrogId="17214" specielId="79368">
<certifikat certifikatId="1234" gyldigt="0"/>
<baadtype baadtypeId="146" baadtypeNavn="GRANADA 31" baadStatus="dhkolbaad" dhKlasseStatus="skrogmedspeciel" dhBeregnes="1" bem="Granada 311 har samme skrog som Granada 31. De første ca. 35 var med blykøl (KC 0,96) - senere kommer det med jernkøl (KC 1,17)" skrogId="17214" specielId="79368"/>
<rigsejl RigsejlId="28158" E="3.27" P="11.14" HB="0.15" MGM="2.10" MGU="1.21" Tmax="12.10" LP="5.23" FSP="0.08" SPL="0.00" J="3.49" TPS="0.00" JHW="0.00" ISP="12.38" SL="0.00" SLU="0.00" SLE="0.00" SF="0.00" SMG="0.00" SFA="0.00" SMGA="0.00" PropelId="2" propel="Fri foldepropel med 2-3 blade (2)" RF="0" MF="0" HF="0"/>
<skrog SkrogId="17214" Gmax="6.42" SGmax="5.59" FBSB="0.93" FBBB="0.94" SBmax="5.73" UDFSB="0.26" UDFBB="0.26" OF="1.17" OA="0.00" UDHBmax="0.00" UDHmax="0.00" STF="1.11" AF="0.00"/>
<speciel SpecielId="79368" Bmax="3.05" LOA="9.38" D="4.17" K="1.80" KC="0.96" KFId="1" KF="Normal" KontrolVejet="0" KontrolMaalt="0" KontrolKrenget="0"/>
<ejer personId="53371" klubId="318" klubNavn="Aarhus Sejlklub" arrangor="1"/>
<offentligmaaler personId="32217" aktiv="0" maaHentePersonlister="0" offentlig="0" sejlmager="0" maalerbem="" navn="Niels Agerskov"/>
<beregning baadId="1234" status="beregnet" oprettetTid="2019-05-10T12:15:32.637" beregnetTid="2019-05-10T12:16:31.447" beregnetOATid="43595.5115" beskedSendes="1"/>
</baad>
</websejler>
I expect to get "Ongo Bongo" as result.
$url = "https://xml.websejler.dk/da/baad/1234";
$xml = simplexml_load_file($url);
echo (string) $xml->baad->attributes()['baadNavn']; // Ongo Bongo
You can use the attributes() function on the node to get it's attributes:
$xml_str = '<xml>
<node>
<someTag cp="c2">content</someTag>
</node>
</xml>';
$res = simplexml_load_string($xml_str);
$items = $res->xpath("//someTag");
var_dump((string) $items[0]->attributes()->cp);
The returned element is an SimpleXMLElement, so in order to use it I converted it to string (using the (string) cast).
I have an object $myObject; I'm trying to return php object as json, but it loses some data. This is how $myObject looks:
CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList Object
(
[priceListId:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => 32
[amounts:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
(
[0] => 1000
[1] => 2000
[2] => 3000
[3] => 4000
[4] => 5000
[5] => 6000
[6] => 7000
)
[amountsKeys:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
(
[1000] => 0
[2000] => 1
[3000] => 2
[4000] => 3
[5000] => 4
[6000] => 5
[7000] => 6
)
[periods:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
(
[0] => 10
[1] => 15
[2] => 20
[3] => 25
[4] => 30
)
[periodsKeys:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
(
[10] => 0
[15] => 1
[20] => 2
[25] => 3
[30] => 4
)
[amount:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => 7000
[period:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => 30
[prices:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
(
[30] => Array
(
[7000] => CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price Object
(
[period:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 30
[amount:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 7000
[charge:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 1580
[interest:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[administrativeFee:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[payment:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[annualPercentageRate:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 1089.6
[annualInterestRate:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 274.62
[schedule:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] =>
)
)
)
[settings:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings Object
(
[mode:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings:private] => credits
[preset:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings:private] =>
[implementation:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings:private] => matrix
[defaults:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings:private] => Array
(
[period] =>
[amount] =>
)
)
)
After json_encode($myObject); new data looks like this (screenshot for better json view):
screenshot of returned json
Why information I wrote below is missing and how to access it?
Missing stuff:
[7000] => CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price Object
(
[period:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 30
[amount:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 7000
[charge:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 1580
[interest:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[administrativeFee:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[payment:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[annualPercentageRate:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 1089.6
[annualInterestRate:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 274.62
[schedule:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] =>
)
The problem is json_encode can not access private properties.
Here are some workarounds you could do:
Convert the properties to be public
You could use a reflection class to convert the properties accessibility before you convert it
Example:
$refObject = new ReflectionObject( $obj );
$refProperty = $refObject->getProperty( 'property' );
$refProperty->setAccessible( true );
You could loop through the properties of the object using a Reflection object and call all the getters converting the object into an array structure that could be easily translated
Refelection Class Documentation
I have json return string like given below. I want to extract cancellation Policy list of objects like cutoff Time and refund In Percentage. I tried using for-loop but it didn't help me. Can you please help me on extracting this.
Array (
[apiStatus] => Array ( [success] => 1 [message] => SUCCESS ) <br>
[apiAvailableBuses] => Array ( <br>
[0] => Array ( [droppingPoints] => [availableSeats] => 41 <br>[partialCancellationAllowed] => [arrivalTime] => 08:00 AM <br>
[cancellationPolicy] => [<br>
{"cutoffTime":"1","refundInPercentage":"10"},<br>
{"cutoffTime":"2","refundInPercentage":"50"},<br>
{"cutoffTime":"4","refundInPercentage":"90"}<br>
] <br>
[boardingPoints] => Array ( [0] => Array ( [time] => 09:00PM [location] => Ameerpet,|Jeans Corner 9687452130 [id] => 6 ) [1] => Array ( [time] => 09:15PM [location] => S.R Nagar,S.R Nagar [id] => 2224 ) [2] => Array ( [time] => 09:10PM [location] => Kondapur,Toyota Show room [id] => 2244 ) ) [operatorName] => Deepak Travels [departureTime] => 9:00 PM [mTicketAllowed] => [idProofRequired] => [serviceId] => 6622 [fare] => 800 [busType] => 2+1 Hi-Tech Non A/c [routeScheduleId] => 6622 [commPCT] => 0 [operatorId] => 213 [inventoryType] => 0 ) <br>
[1] => Array ( [droppingPoints] => [availableSeats] => 41 [partialCancellationAllowed] => [arrivalTime] => 07:00 AM <br>
[cancellationPolicy] => [<br>
{"cutoffTime":"1","refundInPercentage":"10"},<br>
{"cutoffTime":"2","refundInPercentage":"50"},<br>
{"cutoffTime":"4","refundInPercentage":"90"}<br>
] <br>
[boardingPoints] => Array ( [0] => Array ( [time] => 09:10PM [location] => Ameerpet,|Jeans Corner [id] => 6 ) [1] => Array ( [time] => 09:00PM [location] => S.R Nagar,S.R Nagar [id] => 2224 ) [2] => Array ( [time] => 08:30PM [location] => KUKATPALLY,JNTU [id] => 2230 ) ) [operatorName] => Dhanunjayabus [departureTime] => 9:00 PM [mTicketAllowed] => [idProofRequired] => [serviceId] => 6743 [fare] => 900 [busType] => VOLVO [routeScheduleId] => 6743 [commPCT] => 0 [operatorId] => 233 [inventoryType] => 0 )
)
)
Use a foreach() for it like so:
foreach ($your_response['apiAvailableBuses'] as $el) {
$cancellationPolicy[] = $el['cancellationPolicy'];
}
Try this:
foreach($data['apiStatus']['apiAvailableBuses'] as $item) {
foreach($item['cancellationPolicy'] as $key => $json) {
$jsonDecoded = json_decode($json, true);
// And you will have access to the data like this
// $jsonDecoded['cutoffTime'];
// $jsonDecoded['refundInPercentage'];
}
}
$response = json_decode($apiResponse);
$cancellationPolicies = [];
foreach ($response->apiAvailableBuses as $availableBus) {
$cancellationPolicies[] = $availableBus['cancellationPolicy'];
// if you want to display something you can simply do it like this;
echo $availableBus['cancellationPolicy']->cutoffTime;
}
This question already has answers here:
How to convert XML into array in PHP?
(12 answers)
Closed 7 years ago.
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);
}
}