Skip to content

Instantly share code, notes, and snippets.

@earth3300
Last active November 8, 2018 16:51
Show Gist options
  • Select an option

  • Save earth3300/d938a3bde774a65f8dbd2c8db3146e67 to your computer and use it in GitHub Desktop.

Select an option

Save earth3300/d938a3bde774a65f8dbd2c8db3146e67 to your computer and use it in GitHub Desktop.
Convert an XML string into an array via json_encode and json_decode.
/**
* Convert String to XML to JSON to Array
*
*/
private function xmlStringToArray( $str )
{
/** Load the string into xml. */
$xml = simplexml_load_string( $str, "SimpleXMLElement", LIBXML_NOCDATA );
/** Encode the xml as JSON */
$json = json_encode( $xml );
/** Decode the $json into an array */
$arr = json_decode( $json, TRUE );
/** Basic check */
if ( is_array( $arr ) )
{
/** Return the array. */
return $arr;
}
else
{
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment