I am storing an xml response in array format. Now i need to get the node attribute values like HotelLocation, hotelCode etc from the array. Below is the format how my array looks like :
Array
(
[0] => Array
(
[media] => <SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"><SOAP:Body><hotel:HotelMediaLinksRsp TraceId="trace" TransactionId="69B008C27F00000109CA96A4918F80C6" ResponseTime="77" xmlns:common_v25_0="http://www.travelport.com/schema/common_v25_0" xmlns:hotel="http://www.travelport.com/schema/hotel_v25_0"><hotel:HotelPropertyWithMediaItems><hotel:HotelProperty HotelChain="CP" HotelCode="36588" HotelLocation="DXB" Name="CROWNE PLAZA FESTIVAL CITY" VendorLocationKey="1tTgCxLKRUuwf3+sFMCwFg==" HotelTransportation="Limo" ReserveRequirement="Other" ParticipationLevel="Enhanced Best Available Rate participant" Availability="Available" FeaturedProperty="true" NetTransCommissionInd="C"><hotel:PropertyAddress><hotel:Address>DUBAI-FESTIVAL CITY</hotel:Address></hotel:PropertyAddress></hotel:HotelProperty></hotel:HotelPropertyWithMediaItems></hotel:HotelMediaLinksRsp></SOAP:Body></SOAP:Envelope>
)
)
(Php)
First you need to access that part of your array.
If it is stored as $MyResponse you would access [0] using $MyResponse[0].
I would then turn the XML into an array to make it easier to access like so. See more on SimpleXML here.
$xml = new SimpleXMLElement($MyResponse[0]);
Then you can access the individual elements like an array.
For Example..
echo $xml->a->b->b['name'];
Related
I am trying to take an array of filenames and output the following...
a:1:{s:4:"docs";a:4:{i:0;a:1:{s:15:"property_imgurl";s:63:"http://wwww.example.com/image1.jpg";}i:1;a:1:{s:15:"property_imgurl";s:63:"http://wwww.example.com/image2.jpg";}i:2;a:1:{s:15:"property_imgurl";s:63:"http://wwww.example.com/image3.jpg";}i:3;a:1:{s:15:"property_imgurl";s:63:"http://wwww.example.com/image4.jpg";}}}
This is what I have so far...
<?php
$serialized_data = serialize(array('http://www.example.com/image1.jpg', 'http://www.example.com/image2.jpg', 'http://www.example.com/image3.jpg', 'http://www.example.com/image4.jpg'));
echo $serialized_data . '<br>';
?>
But this is giving me...
a:4:{i:0;s:34:"http://www.example.com/image1.jpg";i:1;s:34:"http://www.example.com/image2.jpg";i:2;s:34:"http://www.example.com/image3.jpg";i:3;s:34:"http://www.example.com/image4.jpg";}
Where am I going wrong?
There is nothing wrong with the serialized array. You're just not creating the array like you want it to be. PHP can't guess how you really want your array to be, so you have to tell PHP how you want it to be. So what you need to do is to change the input array correctly.
You're giving
array('http://www.example.com/image1.jpg', 'http://www.example.com/image2.jpg', 'http://www.example.com/image3.jpg', 'http://www.example.com/image4.jpg')
and that's completely different from the serialized array how it should be. Your Array needs to look like this
array('docs' => array(array('property_imgurl' => 'http://www.example.com/image1.jpg'), array('property_imgurl' => 'http://www.example.com/image2.jpg'), array('property_imgurl' => 'http://www.example.com/image3.jpg'), array('property_imgurl' => 'http://www.example.com/image4.jpg')))
Look at this eval
You're just missing the array key definitions.
$serialized = array(array('docs' => array(array('property_imgurl' => 'http://www.example.com/image4.jpg'))));
As you can see, each URL has a key of property_imgurl and each of those array is part of a parent array with a key of docs
Here's the eval.in
while I was searching through some file in a php library, i found some documents like this
a:1:{i:0;a:1:{s:3:"cnt";s:1:"1";}}
This is definitely not JSON. Does any one know what is this? or is it a custom syntax for the guy who wrote the library ?
That is a serialized data and not any programming language syntax.
For your understanding...
<?php
$arr = ['a'=>1,'b'=>2,'cnt'=>5];
echo serialize($arr);
OUTPUT :
a:3:{s:1:"a";i:1;s:1:"b";i:2;s:3:"cnt";i:5;}
Language in Php more info on this link unserialize
This is basically a serailize form of array
of follwoing array
Array
(
[0] => Array
(
[cnt] => 1
)
)
You can get it back by into array
$a = 'a:1:{i:0;a:1:{s:3:"cnt";s:1:"1";}}';
$unserailize_a = (unserialize($a));
To convert an array into string us the Serialize
I'm beginning to use simplexml and I don't know what I'm doing wrong .. :(
I want to extract a xml from a url, I'm using the following code:
$xml = new SimpleXMLElement(file_get_contents($url));
And... print_r shows other object SimpleXML inside my array...It is correct? How can I read it?
SimpleXMLElement Object
(
[action] => QUERY
[response] => SUCCESS
[responsedata] => SimpleXMLElement Object
(
)
)
Thanks a lot,
SimpleXMLElement::__construct provides an optional (3rd) argument called data_is_url for cases like yours.
By default, data_is_url is FALSE. Use TRUE to specify that data is a path or URL to an XML document instead of string data.
You should try something like:
$xml = new SimpleXMLElement($url, 0, true);
I am currently attempting to extract values from an array that was generated by an SIMPLEXML/XPATH Query. I have had no success with my attempts if anyone can take look would be greatly appreciated.
I am looking just to extract ReclaimDate. I have tried a few functions and some of the advice on this post with no luck.
Array
(
[0] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ReclaimDate] => 05/15/2008
[ReclaimPrice] => 555555555
[_Owner] => ownername
)
)
)
If I just had to take a stab, I'd agree that what #Frank Farmer said should work:
// if $myVar is what you print_r'ed
echo (string)$myVar[0][0]['ReclaimDate'];
or this
echo (string)$myVar[0][0]->attributes('ReclaimDate');
http://www.php.net/manual/en/simplexml.examples-basic.php#example-4587
This is resolved thanks to Frank Farmer and Dan Beam.
This worked : echo (string)$check_reo[0][0]['ReclaimDate']
For anyone that is looking to use SimpleXML and XPATH to extract and write some basic logic from an XML file this is what worked for me.
$xmlstring = <<<XML <?xml version='1.0' standalone='yes'?> <YOURXMLGOESHERE>TEST</YOURXMLGOESHERE> XML;
$xpathcount = simplexml_load_string($xmlstring); // Load XML for XPATH Node Counts
$doc = new DOMDocument(); // Create new DOM Instance for Parsing
$xpathcountstr = $xpathcount->asXML(); // Xpath Query
$doc->loadXML($xpathcountstr); // Load Query Results
$xpathquery = array($xpathcount->xpath("//XMLNODEA[1]/XMLNODEB/*[name()='KEYWORDTOCHECKIFXMLCEXISTS']"));
print_r ($xpathquery) // CHECK Array that is returned from the XPATH query
`Array
(
[0] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ReclaimDate] => 05/15/2008
[ReclaimPrice] => 555555555
[_Owner] => ownername
)
)
) // Array RETURNED`
echo (string)$xpathquery[0][0]['ReclaimDate'] // EXTRACT THE VALUE FROM THE ARRAY COLUMN;
This site helped me receive a better understanding on how XPATH can search XML very easily with a lot more features than what I had previously known.
http://zvon.org/xxl/XPathTutorial/Output/examples.html
Here is the simple XML Function that worked for me
$xmlread = simplexml_load_string($xmlstring, "simple_xml_extended");
`class simple_xml_extended extends SimpleXMLElement { // Read XML and get attribute
public function Attribute($name){
foreach($this->Attributes() as $key=>$val) {
if($key == $name)
return (string)$val;
}
}
}`
Here is the Function action when extracting Single Values with an attribute based XML results
$GETVAR1 = $xmlread->XMLNODE1->XMLNODE2->XMLNODE3->XMLNODE4->XMLNODE5[0]->Attribute('XMLNODE5ATTRIBUTE');
This might not be the most efficient or best method, but its what ended working our for me. Hope this helps someone out who is still unclear about SIMPLEXML and XPATH.
An additional link for further insight: http://www.phpfreaks.com/tutorial/handling-xml-data
I have a multi-dimensional multi-object array from a simplexml_import_dom() function call.
A slice of one section of the array:
[Price] => SimpleXMLElement Object
(
[Prices] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[MType] => A
[PType] => R
This is causing me quite a bit of problems when trying to read nested objects. I have tried to loop through the array using multiple get_object_vars() but because the depth and location of the nested objects is continually changing I haven't been able to yield desirable results.
Does PHP contain a function that I haven't been able to find to convert a multi-dimensional multi-object array to a standard multi-dimensional array? Or has anyone solved this problem before?
Thanks for your help!
The question is often asked, but there's a fundamental issue that has to be addressed on a case-by-case basis when converting a XML tree to an array, which makes an one-size-fits-all method impossible: how do you differentiate nodes from attributes?
For instance, how would you convert this XML to an array:
<node val="attr"><val>child</val></node>
Also, a node can have any number of children with the same name, which you can't emulate with an associative array.
Long story short, you'll have to cook up your own solution. Judging from your output, it would look something like this:
$arr = array();
foreach ($Price->Prices as $Prices)
{
$tmp = array();
foreach ($Prices->attributes() as $k => $v)
{
$tmp[$k] = (string) $v;
}
$arr[] = $tmp;
}
If it's not what you're looking for, please edit your question and add an example of the source document (XML) as well as the expected result (the array.)
Are you aware that these objects have functions that you can use? Try the following:
foreach ($simpleXmlObject->children() as $element) {
echo $element->getName();
}