I want to convert SimpleXMLElement Object to XML in php - php

I have created an array from simplexml_load_string and now I want to convert back it to XML
I had tried
function convertSimpleXMLToArray($xml)
{
if(class_basename($xml) == 'SimpleXMLElement')
{
$xml = get_object_vars($xml);
foreach($xml as $key => $value)
{
if(class_basename($value) == 'SimpleXMLElement') $xml[$key] = convertSimpleXMLToArray($value);
}
}
return $xml;
}
$result = convertSimpleXMLToArray($xml);
and this
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($test_array, array ($xml, 'addChild'));
print $xml->asXML();
function array2xml($array, $xml = false){
if($xml === false){
$xml = new SimpleXMLElement('<root/>');
}
foreach($array as $key => $value){
if(is_array($value)){
array2xml($value, $xml->addChild($key));
}else{
$xml->addChild($key, $value);
}
}
return $xml->asXML();
}
header('Content-type: text/xml');
print array2xml($test_array);
This is the part of array which I want to convert back in XML
Array ( [0] => SimpleXMLElement Object ( [uid] => S55 [type] => PACKAGE [serviceCode] => SLVKNKPRE [serviceCodeCMS] => SLVKNKBAI [name] => PREMIUM Experience the Northern Rhine - intern. ship [fromDate] => 2019-09-07T00:00:00.000+02:00 [endDate] => 2019-09-14T00:00:00.000+02:00 [dayService] => false [price] => SimpleXMLElement Object ( [totalPrice] => 1699.00 [totalPriceCurrency] => EUR [basePrice] => 1699.00 [basePriceCurrency] => EUR ) [isAlternativeSearch] => false [externalData] => SimpleXMLElement Object ( [extType] => P [extCategory] => 19941 [externalID] => SimpleXMLElement Object ( ) [touroperatorServiceId] => 19941 ) [destination] => SimpleXMLElement Object ( [destinationCode] => Rhine [destinationTypecode] => RIVER [destinationComposedDesc] => Rhine ) [serviceDescription] => SimpleXMLElement Object ( [description] => PREMIUM Experience the Northern Rhine - intern. ship ) [isDetailsIncluded] => false [duration] => 7 [hasRelatedServices] => false [services] => SimpleXMLElement Object ( [cruiseService] => SimpleXMLElement Object ( [uid] => S56 [type] => CRUISE [serviceCode] => SLVKNKPRE [serviceCodeCMS] => SLVKNKBAI [name] => A-ROSA SILVA PREMIUM Experience the Northern Rhine-int. ship [fromDate] => 2019-09-07T00:00:00.000+02:00 [endDate] => 2019-09-14T00:00:00.000+02:00 [dayService] => false [units] => SimpleXMLElement Object ( [uid] => U28 [category] => A [fromDate] => 2019-09-07T00:00:00.000+02:00 [endDate] => 2019-09-14T00:00:00.000+02:00 [freeUnits] => 4 [numberOfUnits] => 0 [occupancy] => SimpleXMLElement Object ( [occupancy] => 2 [occupancyMin] => 1 [occupancyMax] => 2 [occupancyDef] => 2 ) [totalPrice] => SimpleXMLElement Object ( [totalPrice] => 1699.00 [totalPriceCurrency] => EUR [basePrice] => 1699.00 [basePriceCurrency] => EUR [priceMode] => PERSON )

Related

get std class value from an std classin php

stdClass Object
(
[id_pro_alert] => 155
[User_Id] => 252
[Pro_Id] => 329
[alertmethod] => daily
[Alertname] => xcxv
[name] => cyriac
[email] => cyriac#tewve.com
[date] => 2016-21-10 09:20:26
[mobile] =>
[comments] =>
[Alert_Id] => 329
[Prop_Purpose] => lease
[Min_Area] => 500
[Max_Area] => 1000
[Area_unit] => Sq.ft
[Min_cost] => 1000
[Max_cost] => 10000
[City] => kochi
[Create_Date] => 2016-10-17 12:59:14
[Update_Date] => 2016-10-17 12:59:14
[Status] => Active
[0] => stdClass Object
(
[Prop_type] => Agricultural Land
)
[1] => stdClass Object
(
[Prop_type] => Office in IT park
)
[2] => stdClass Object
(
[Prop_type] => Business center
)
)
How to get Prop_type value from stdclass
Check this, You can access all Prop_type's
foreach($data as $key=>$row){
if (is_int($key)) {
echo $row->Prop_type;
}
}
Create array of Prop_type's
$propData =array();
foreach($data as $key=>$row){
if (is_int($key)) {
$propData[] = $row->Prop_type;
}
}
echo $array->0->Prop_type;

how to convert xml array to php array [duplicate]

This question already has an answer here:
How to convert xml into array in php?
9 answers
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);
}
}

echo json object $stdClass Object array

echo "<pre>"; print_r($data); echo "</pre>";
Gives following output:
$stdClass Object
(
[cartName] => AngularStore
[clearCart] =>
[checkoutParameters] => stdClass Object
(
)
[items] => Array
(
[0] => stdClass Object
(
[sku] => 01
[name] => Product 1
[price] => 600
[quantity] => 1
[stock] => 5
[scheme] => Array
(
[0] => stdClass Object
(
[name] => offerAB
[desc] => Description on the scheme
[no] => 3
[$$hashKey] => 01O
[checked] => 1
)
[1] => stdClass Object
(
[name] => offerXY
[desc] => Description on the scheme
[no] => 5
[$$hashKey] => 01P
)
[2] => stdClass Object
(
[name] => OfferPQ
[desc] => Description on the scheme
[no] => 2
[$$hashKey] => 01Q
[checked] => 1
)
[3] => stdClass Object
(
[name] => OfferLM
[desc] => Description on the scheme
[no] => 4
[$$hashKey] => 01R
)
)
[$$hashKey] => 05V
)
[1] => stdClass Object
(
[sku] => 02
[name] => Product 2
[price] => 500
[quantity] => 1
[stock] => 400
[scheme] => Array
(
[0] => stdClass Object
(
[name] => offerAB
[desc] => Description on the scheme
[no] => 6
[$$hashKey] => 01W
)
[1] => stdClass Object
(
[name] => offerXY
[desc] => Description on the scheme
[no] => 7
[$$hashKey] => 01X
)
[2] => stdClass Object
(
[name] => OfferPQ
[desc] => Description on the scheme
[no] => 3
[$$hashKey] => 01Y
)
[3] => stdClass Object
(
[name] => OfferLM
[desc] => Description on the scheme
[no] => 8
[$$hashKey] => 01Z
)
)
[$$hashKey] => 05W
)
)
[qty] => 3
)
I want to print value of sku , name, price using foreach loop
Since i m new to it i first started printing a single value
echo $data->items->arr[0]->sku;
Notice: Trying to get property of non-object getting this error
but i want to print the values in foreach please help!
Items is a property of the main object, and in itself is an array. This is what you're after:
foreach($data->items as $d) {
echo $d->name, '<br />', $d->sku, '<br />', $d->price;
}
If you want to access one of those element without a loop, you need to provide the array index, for example:
echo $data->items[0]->name
the easy way for you is convert object to array
function array2object($array) {
if (is_array($array)) {
$obj = new StdClass();
foreach ($array as $key => $val){
$obj->$key = $val;
}
}
else { $obj = $array; }
return $obj;
}
function object2array($object) {
if (is_object($object)) {
foreach ($object as $key => $value) {
$array[$key] = $value;
}
}
else {
$array = $object;
}
return $array;
}
// example:
$array = array('foo' => 'bar', 'one' => 'two', 'three' => 'four');
$obj = array2object($array);
print $obj->one; // output's "two"
$arr = object2array($obj);
print $arr['foo']; // output's bar
foreach($data['items'] as $item) {
echo $item['sku'].PHP_EOL
echo $item['name'].PHP_EOL
echo $item['price'].PHP_EOL;
}

How to access #attributes data in SimpleXMLElement?

I am trying to get some specific data from a xml file.
php Code
$url = 'http://amdata.adlibsoft.com/wwwopac.ashx?
database=AMcollect&search=priref=397*&xmltype=grouped';
$xml = file_get_contents($url);
$xml = new SimpleXMLElement($xml);
doing
print_r ($xml);
will return
SimpleXMLElement Object
(
[recordList] => SimpleXMLElement Object
(
[record] => SimpleXMLElement Object
(
[#attributes] => Array
(
[priref] => 397
[created] => 2013-11-25T14:03:22
[modification] => 2013-11-25T18:01:23
[selected] => False
)
[acquisition.date] => 1860-12-24
[acquisition.method] => legaat
[collection] => Fodor, collectie Carel Joseph
[credit_line] => Amsterdam Museum, legaat C.J. Fodor
[dimension] => Array
(
[0] => SimpleXMLElement Object
(
[dimension.part] => SimpleXMLElement Object
(
)
[dimension.type] => hoogte a
[dimension.unit] => cm
[dimension.value] => 65.5
)
[1] => SimpleXMLElement Object
(
[dimension.part] => SimpleXMLElement Object
(
)
[dimension.type] => breedte a
[dimension.unit] => cm
[dimension.value] => 97.5
)
[2] => SimpleXMLElement Object
(
[dimension.part] => SimpleXMLElement Object
(
)
[dimension.type] => hoogte b
[dimension.unit] => cm
[dimension.value] => 51.3
)
[3] => SimpleXMLElement Object
(
[dimension.part] => SimpleXMLElement Object
(
)
[dimension.type] => breedte b
[dimension.unit] => cm
[dimension.value] => 84.1
)
[4] => SimpleXMLElement Object
(
[dimension.part] => SimpleXMLElement Object
(
)
[dimension.type] => hoogte c
[dimension.unit] => cm
[dimension.value] => 40.4
)
[5] => SimpleXMLElement Object
(
[dimension.part] => SimpleXMLElement Object
(
)
[dimension.type] => breedte c
[dimension.unit] => cm
[dimension.value] => 80.7
)
)
[maker] => Array
(
[0] => SimpleXMLElement Object
(
[creator] => Kaiser, Johann Wilhelm (I)
[creator.date_of_birth] => 1813-01-05
[creator.date_of_death] => 1900-11-29
[creator.qualifier] => SimpleXMLElement Object
(
)
[creator.role] => graveur
)
[1] => SimpleXMLElement Object
(
[creator] => Helst, Bartholomeus van der
[creator.date_of_birth] => 1613
[creator.date_of_death] => 1670
[creator.qualifier] => naar
)
[2] => SimpleXMLElement Object
(
[creator] => Kunsthandel Frans Buffa & Zonen
[creator.date_of_birth] => SimpleXMLElement Object
(
)
[creator.date_of_death] => SimpleXMLElement Object
(
)
[creator.qualifier] => SimpleXMLElement Object
(
)
[creator.role] => uitgever
)
)
[material] => papier
[object_category] => prentencollectie
[object_name] => Array
(
[0] => gravure
[1] => ets
[2] => prent
)
[object_number] => A 11259
[part_of_reference] => KA 22389 & A 11217 t/m A 11265
[priref] => 397
[production.date.end] => 1860
[production.date.start] => 1849
[technique] => gegraveerd
[title] => De schuttersmaaltijd
)
)
[diagnostic] => SimpleXMLElement Object
(
[hits] => 1
[xmltype] => Grouped
[link_resolve_time] => 15.5801
[first_item] => 1
[search] => priref Equals 397*
[sort] => SimpleXMLElement Object
(
)
[limit] => 1
[hits_on_display] => 1
[response_time] => 0
[xml_creation_time] => 15.5801
[dbname] => collect
[dsname] => intern
[cgistring] => SimpleXMLElement Object
(
[param] => AMcollect
)
)
)
Now let's say I want to get the priref I tried the following things
echo($xml->record->priref);
echo $xml->record['priref'];
Both gave no result(no error and nothing being displayed)
then I tried
echo $xml->record->attributes()->priref;
and got "Node no longer exists"
This will get you attributes array:
echo $xml->recordList->record[0]->attributes();
Make use of a foreach and access it like the key-value pair to display the needed value.
<?php
$url = 'http://amdata.adlibsoft.com/wwwopac.ashx?
database=AMcollect&search=priref=397*&xmltype=grouped';
$xml = file_get_contents($url);
$xml = simplexml_load_string($xml);
echo "<pre>";
foreach ($xml->recordList->record->attributes() as $k=>$a)
{
if($k=='priref') { echo $a; break;}
}
OUTPUT :
397

If array equal array PHP

Is possible to make if($array1[0]=>somevalue == $array2[0]=>somevalue){echo true;}else{echo false;}? because I cant make it work, can you help me please,
Example:
$array1([0] => 'Joe', [2] => 'Pairs');
$array2([0] => 'info' => array([0] => 'joe'));
foreach($array2->info as $info){
foreach($array1 as $name){
if($name == $info[0]){
echo 'true';
}
else{
echo 'false';
}
}
}
And this is the real code,
The arrays:
Array
(
[0] => SimpleXMLElement Object
(
[companyLocationInfo] => Array
(
[0] => SimpleXMLElement Object
(
[companyName] => AVIS
[name] => NYCC07
[line1] => 420 EAST 90TH STREET
)
[2] => SimpleXMLElement Object
(
[companyName] => AVIS
[name] => NYCC06
[line1] => 310 EAST 64TH STREET
)
[3] => SimpleXMLElement Object
(
[companyName] => AVIS
[name] => NYCC01
[line1] => 68 EAST 11TH STREET
)
)
[rates] => Array
(
[0] => SimpleXMLElement Object
(
[pickupDropoffLocations] => Array
(
[0] => SimpleXMLElement Object
(
[companyName] => AVIS
[name] => NYCC07
)
)
[vehicleRentalPrefType] => CCAR
[rateAmount] => 83.99
[rateCurrency] => USD
)
[2] => SimpleXMLElement Object
(
[pickupDropoffLocations] => Array
(
[0] => SimpleXMLElement Object
(
[companyName] => AVIS
[name] => NYCC06
)
)
[vehicleRentalPrefType] => CCAR
[rateAmount] => 110.54
[rateCurrency] => USD
)
[3] => SimpleXMLElement Object
(
[pickupDropoffLocations] => Array
(
[0] => SimpleXMLElement Object
(
[companyName] => AVIS
[name] => NYCC01
)
)
[vehicleRentalPrefType] => CCAR
[rateAmount] => 210.65
[rateCurrency] => USD
)
)
)
)
and this is the code:
$results_array = array();
foreach($result[0]->rates as $rate) {
foreach($result[0]->companyLocationInfo as $info) {
if($info->name == $rate->pickupDropoffLocations[0]->name) {
$results_array[] = array(
'line1' => $info->line1,
'name' => $info->locationDetails->name,
'companyName' => $info->companyName,
'vehicleRentalPrefType' => $rate->vehicleRentalPrefType
);
}
}
}
print_r($results_array);
Thank you.
What you're trying to do should be possible. If the code above is what you're actually trying to run, then your first problem is that you're comparing the values "Joe" and "joe" and those are not the same.
If you want a case-insensitive comparison, use if (strcasecmp($var1, $var2) == 0) (it returns zero if the two strings are the same, without considering case).

Resources