Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ederrafo/07be39b273616e47fae7 to your computer and use it in GitHub Desktop.
Save ederrafo/07be39b273616e47fae7 to your computer and use it in GitHub Desktop.
$xml_string = '
<getHotelAvailRequest>
<destination>130336</destination>
<checkin>14-02-2015</checkin>
<checkout>16-02-2015</checkout>
<nationality>136</nationality>
<listrooms>
<item>
<count>2</count>
<adults>1</adults>
<children>1</children>
<childrenages>
<item><age>1</age>
</item>
</childrenages>
</item>
<item>
<count>1</count>
<adults>2</adults>
<children>1</children>
<childrenages>
<item><age>1</age></item>
</childrenages>
</item>
</listrooms>
<language>en</language>
</getHotelAvailRequest>
';
/*
ENT_QUOTES - Encodes double and single quotes
*/
echo "FILE: ",__FILE__,"<br>LINE: ", __LINE__,"<pre>", htmlspecialchars($xmlString, ENT_QUOTES); echo "</pre>";
/* Convert XML string to SimpleXMLElement Object */
$simple_xml_object = simplexml_load_string($xml_string);
echo "FILE: ",__FILE__,"<br>LINE: ", __LINE__,"<pre>"; print_r($simple_xml_object); echo "</pre>";
/* Convert to json string*/
$json = json_encode($simple_xml_object);
echo "FILE: ",__FILE__,"<br>LINE: ", __LINE__,"<pre>"; print_r($json); echo "</pre>";
/*
Convert to array.
TRUE: delete objects
*/
$array = json_decode($json, TRUE);
echo "FILE: ",__FILE__,"<br>LINE: ", __LINE__,"<pre>"; print_r($array); echo "</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment