|
| 1 | +XML to PHP array convertor |
| 2 | +========================== |
| 3 | + |
| 4 | +Smart tool to convert your XML to PHP array. |
| 5 | + |
| 6 | +This is fork from [gaarf/XML-string-to-PHP-array](https://github.com/gaarf/XML-string-to-PHP-array). |
| 7 | + |
| 8 | +Install and simply use |
| 9 | +---------------------- |
| 10 | + |
| 11 | +Use Composer: |
| 12 | + |
| 13 | +```shell |
| 14 | +composer require baraja-core/xml-to-php-array |
| 15 | +``` |
| 16 | + |
| 17 | +And then package will be automatically installed to your project and you can simply call: |
| 18 | + |
| 19 | +```php |
| 20 | +$resultArray = Convertor::covertToArray($xml); |
| 21 | +``` |
| 22 | + |
| 23 | +Documentation |
| 24 | +------------- |
| 25 | + |
| 26 | +One common need when working in PHP is a way to convert an XML document |
| 27 | +into a serializable array. If you ever tried to serialize() and then |
| 28 | +unserialize() a SimpleXML or DOMDocument object, you know what I’m |
| 29 | +talking about. |
| 30 | + |
| 31 | +Assume the following XML snippet: |
| 32 | + |
| 33 | +```xml |
| 34 | +<tv> |
| 35 | + <show name="Family Guy"> |
| 36 | + <dog>Brian</dog> |
| 37 | + <kid>Chris</kid> |
| 38 | + <kid>Meg</kid> |
| 39 | + </show> |
| 40 | +</tv> |
| 41 | +``` |
| 42 | + |
| 43 | +There’s a quick and dirty way to do convert such a document to an array, |
| 44 | +using type casting and the JSON functions to ensure there are no exotic |
| 45 | +values that would cause problems when unserializing: |
| 46 | + |
| 47 | +```php |
| 48 | +$a = json_decode(json_encode((array) Convertor::covertToArray($s)), true); |
| 49 | +``` |
| 50 | + |
| 51 | +Here is the result for our sample XML, eg if we `print_r($a)`: |
| 52 | + |
| 53 | +``` |
| 54 | +Array |
| 55 | +( |
| 56 | + [show] => Array |
| 57 | + ( |
| 58 | + [@attributes] => Array |
| 59 | + ( |
| 60 | + [name] => Family Guy |
| 61 | + ) |
| 62 | + [dog] => Brian |
| 63 | + [kid] => Array |
| 64 | + ( |
| 65 | + [0] => Chris |
| 66 | + [1] => Meg |
| 67 | + ) |
| 68 | + ) |
| 69 | +) |
| 70 | +``` |
| 71 | + |
| 72 | +Pretty nifty, eh? But maybe we want to embed some HTML tags or something |
| 73 | +crazy along those lines. then we need a CDATA node… |
| 74 | + |
| 75 | +```xml |
| 76 | +<tv> |
| 77 | + <show name="Family Guy"> |
| 78 | + <dog>Brian</dog> |
| 79 | + <kid>Chris</kid> |
| 80 | + <kid>Meg</kid> |
| 81 | + <kid><![CDATA[<em>Stewie</em>]]></kid> |
| 82 | + </show> |
| 83 | +</tv> |
| 84 | +``` |
| 85 | + |
| 86 | +The snippet of XML above would yield the following: |
| 87 | + |
| 88 | +``` |
| 89 | +Array |
| 90 | +( |
| 91 | + [show] => Array |
| 92 | + ( |
| 93 | + [@attributes] => Array |
| 94 | + ( |
| 95 | + [name] => Family Guy |
| 96 | + ) |
| 97 | + [dog] => Brian |
| 98 | + [kid] => Array |
| 99 | + ( |
| 100 | + [0] => Chris |
| 101 | + [1] => Meg |
| 102 | + [2] => Array |
| 103 | + ( |
| 104 | + ) |
| 105 | + ) |
| 106 | + ) |
| 107 | +) |
| 108 | +``` |
| 109 | + |
| 110 | +That’s not very useful. We got in trouble because the CDATA node, a |
| 111 | +SimpleXMLElement, is being cast to an array instead of a string. To |
| 112 | +handle this case while still keeping the nice @attributes notation, we |
| 113 | +need a slightly more verbose conversion function. This is my version, |
| 114 | +hereby released under a do-whatever-but-dont-sue-me license. |
| 115 | + |
| 116 | +The result, for our *Stewie* snippet: |
| 117 | + |
| 118 | +``` |
| 119 | +Array |
| 120 | +( |
| 121 | + [show] => Array |
| 122 | + ( |
| 123 | + [@attributes] => Array |
| 124 | + ( |
| 125 | + [name] => Family Guy |
| 126 | + ) |
| 127 | + [dog] => Brian |
| 128 | + [kid] => Array |
| 129 | + ( |
| 130 | + [0] => Chris |
| 131 | + [1] => Meg |
| 132 | + [2] => <em>Stewie</em> |
| 133 | + ) |
| 134 | + ) |
| 135 | +) |
| 136 | +``` |
| 137 | + |
| 138 | +Victory is mine! :D |
0 commit comments