PHPackages                             gaarf/xml-to-php-array - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. gaarf/xml-to-php-array

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

gaarf/xml-to-php-array
======================

XML to PHP array convertor

v1.0(6y ago)044.2k↓31%2PHP &gt;=7.1.0

Since Oct 8Pushed 5y agoCompare

[ Source](https://github.com/RicardoRamirezR/XML-string-to-PHP-array)[ Packagist](https://packagist.org/packages/gaarf/xml-to-php-array)[ Docs](https://github.com/gaarf/XML-string-to-PHP-array)[ RSS](/packages/gaarf-xml-to-php-array/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (4)Used By (2)

XML to PHP array convertor
==========================

[](#xml-to-php-array-convertor)

Smart tool to convert your XML to PHP array.

Install and simply use
----------------------

[](#install-and-simply-use)

Use Composer:

```
composer require gaarf/xml-to-php-array
```

And then package will be automatically installed to your project and you can simply call:

```
$resultArray = Convertor::covertToArray($xml);
```

Documentation
-------------

[](#documentation)

One common need when working in PHP is a way to convert an XML document into a serializable array. If you ever tried to serialize() and then unserialize() a SimpleXML or DOMDocument object, you know what I’m talking about.

Assume the following XML snippet:

```

		Brian
		Chris
		Meg

```

There’s a quick and dirty way to do convert such a document to an array, using type casting and the JSON functions to ensure there are no exotic values that would cause problems when unserializing:

```
$a = json_decode(json_encode((array) Convertor::covertToArray($s)), true);
```

Here is the result for our sample XML, eg if we `print_r($a)`:

```
Array
(
    [show] => Array
        (
            [@attributes] => Array
                (
                    [name] => Family Guy
                )
            [dog] => Brian
            [kid] => Array
                (
                    [0] => Chris
                    [1] => Meg
                )
        )
)

```

Pretty nifty, eh? But maybe we want to embed some HTML tags or something crazy along those lines. then we need a CDATA node…

```

		Brian
		Chris
		Meg
		Stewie]]>

```

The snippet of XML above would yield the following:

```
Array
(
    [show] => Array
        (
            [@attributes] => Array
                (
                    [name] => Family Guy
                )
            [dog] => Brian
            [kid] => Array
                (
                    [0] => Chris
                    [1] => Meg
                    [2] => Array
                        (
                        )
                )
        )
)

```

That’s not very useful. We got in trouble because the CDATA node, a SimpleXMLElement, is being cast to an array instead of a string. To handle this case while still keeping the nice @attributes notation, we need a slightly more verbose conversion function. This is my version, hereby released under a do-whatever-but-dont-sue-me license.

The result, for our *Stewie* snippet:

```
Array
(
    [show] => Array
        (
            [@attributes] => Array
                (
                    [name] => Family Guy
                )
            [dog] => Brian
            [kid] => Array
                (
                    [0] => Chris
                    [1] => Meg
                    [2] => Stewie
                )
        )
)

```

Victory is mine! :D

---

### Contributions

[](#contributions)

\[clh-code#1\] If a node has attributes, but contains only text, then the output will be an array with both `@content` and `@attributes` keys

\[reggi#4\] store root element tag name in `@root`

\[janbarasek#13\] Add support for PHP 7.1 + better code style.

\[janbarasek#15\] Rewrite repository as Composer package.

\[roland-d#18\] Treat empty node as string

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 73.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

2414d ago

Major Versions

0.9 → v1.02019-10-08

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6526545?v=4)[Ricardo](/maintainers/RicardoRamirezR)[@RicardoRamirezR](https://github.com/RicardoRamirezR)

---

Top Contributors

[![gaarf](https://avatars.githubusercontent.com/u/205004?v=4)](https://github.com/gaarf "gaarf (30 commits)")[![janbarasek](https://avatars.githubusercontent.com/u/4738758?v=4)](https://github.com/janbarasek "janbarasek (8 commits)")[![clh-code](https://avatars.githubusercontent.com/u/1203334?v=4)](https://github.com/clh-code "clh-code (2 commits)")[![roland-d](https://avatars.githubusercontent.com/u/359377?v=4)](https://github.com/roland-d "roland-d (1 commits)")

### Embed Badge

![Health badge](/badges/gaarf-xml-to-php-array/health.svg)

```
[![Health](https://phpackages.com/badges/gaarf-xml-to-php-array/health.svg)](https://phpackages.com/packages/gaarf-xml-to-php-array)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
