{"id":18551963,"url":"https://github.com/baraja-core/xml-to-array","last_synced_at":"2025-06-29T13:04:23.189Z","repository":{"id":48404113,"uuid":"326048560","full_name":"baraja-core/xml-to-array","owner":"baraja-core","description":"Smart tool to convert your XML to PHP array.","archived":false,"fork":false,"pushed_at":"2022-09-12T07:44:04.000Z","size":25,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-16T14:53:17.069Z","etag":null,"topics":["array","casting","convertor","php","php-array","snippet","xml"],"latest_commit_sha":null,"homepage":"https://php.baraja.cz","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/baraja-core.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-01T20:11:31.000Z","updated_at":"2024-04-19T18:11:10.000Z","dependencies_parsed_at":"2022-08-24T08:11:00.243Z","dependency_job_id":null,"html_url":"https://github.com/baraja-core/xml-to-array","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/baraja-core/xml-to-array","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baraja-core%2Fxml-to-array","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baraja-core%2Fxml-to-array/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baraja-core%2Fxml-to-array/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baraja-core%2Fxml-to-array/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/baraja-core","download_url":"https://codeload.github.com/baraja-core/xml-to-array/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baraja-core%2Fxml-to-array/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260570749,"owners_count":23029759,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","casting","convertor","php","php-array","snippet","xml"],"created_at":"2024-11-06T21:11:18.332Z","updated_at":"2025-06-29T13:04:23.139Z","avatar_url":"https://github.com/baraja-core.png","language":"PHP","readme":"XML to PHP array convertor\n==========================\n\nSmart tool to convert your XML to PHP array.\n\nThis is fork from [gaarf/XML-string-to-PHP-array](https://github.com/gaarf/XML-string-to-PHP-array).\n\nInstall and simply use\n----------------------\n\nUse Composer:\n\n```shell\ncomposer require baraja-core/xml-to-php-array\n```\n\nAnd then package will be automatically installed to your project and you can simply call:\n\n```php\n$resultArray = Convertor::covertToArray($xml);\n```\n\nDocumentation\n-------------\n\nOne common need when working in PHP is a way to convert an XML document\ninto a serializable array. If you ever tried to serialize() and then\nunserialize() a SimpleXML or DOMDocument object, you know what I’m\ntalking about.\n\nAssume the following XML snippet:\n\n```xml\n\u003ctv\u003e\n\t\u003cshow name=\"Family Guy\"\u003e\n\t\t\u003cdog\u003eBrian\u003c/dog\u003e\n\t\t\u003ckid\u003eChris\u003c/kid\u003e\n\t\t\u003ckid\u003eMeg\u003c/kid\u003e\n\t\u003c/show\u003e\n\u003c/tv\u003e\n```\n\nThere’s a quick and dirty way to do convert such a document to an array,\nusing type casting and the JSON functions to ensure there are no exotic\nvalues that would cause problems when unserializing:\n\n```php\n$a = json_decode(json_encode((array) Convertor::covertToArray($s)), true);\n```\n\nHere is the result for our sample XML, eg if we `print_r($a)`:\n\n```\nArray\n(\n    [show] =\u003e Array\n        (\n            [@attributes] =\u003e Array\n                (\n                    [name] =\u003e Family Guy\n                )\n            [dog] =\u003e Brian\n            [kid] =\u003e Array\n                (\n                    [0] =\u003e Chris\n                    [1] =\u003e Meg\n                )\n        )\n)\n```\n\nPretty nifty, eh? But maybe we want to embed some HTML tags or something\ncrazy along those lines. then we need a CDATA node…\n\n```xml\n\u003ctv\u003e\n\t\u003cshow name=\"Family Guy\"\u003e\n\t\t\u003cdog\u003eBrian\u003c/dog\u003e\n\t\t\u003ckid\u003eChris\u003c/kid\u003e\n\t\t\u003ckid\u003eMeg\u003c/kid\u003e\n\t\t\u003ckid\u003e\u003c![CDATA[\u003cem\u003eStewie\u003c/em\u003e]]\u003e\u003c/kid\u003e\n\t\u003c/show\u003e\n\u003c/tv\u003e\n```\n\nThe snippet of XML above would yield the following:\n\n```\nArray\n(\n    [show] =\u003e Array\n        (\n            [@attributes] =\u003e Array\n                (\n                    [name] =\u003e Family Guy\n                )\n            [dog] =\u003e Brian\n            [kid] =\u003e Array\n                (\n                    [0] =\u003e Chris\n                    [1] =\u003e Meg\n                    [2] =\u003e Array\n                        (\n                        )\n                )\n        )\n)\n```\n\nThat’s not very useful. We got in trouble because the CDATA node, a\nSimpleXMLElement, is being cast to an array instead of a string. To\nhandle this case while still keeping the nice @attributes notation, we\nneed a slightly more verbose conversion function. This is my version,\nhereby released under a do-whatever-but-dont-sue-me license.\n\nThe result, for our *Stewie* snippet:\n\n```\nArray\n(\n    [show] =\u003e Array\n        (\n            [@attributes] =\u003e Array\n                (\n                    [name] =\u003e Family Guy\n                )\n            [dog] =\u003e Brian\n            [kid] =\u003e Array\n                (\n                    [0] =\u003e Chris\n                    [1] =\u003e Meg\n                    [2] =\u003e \u003cem\u003eStewie\u003c/em\u003e\n                )\n        )\n)\n```\n\nVictory is mine! :D\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaraja-core%2Fxml-to-array","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbaraja-core%2Fxml-to-array","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaraja-core%2Fxml-to-array/lists"}