Last active
November 8, 2018 16:51
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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