PHPackages                             sb15/xml-string-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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. sb15/xml-string-to-php-array

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

sb15/xml-string-to-php-array
============================

xml to array

v0.0.1(11y ago)09.4k2MITShellPHP &gt;=5.3

Since Aug 10Pushed 11y ago1 watchersCompare

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

READMEChangelog (1)DependenciesVersions (3)Used By (2)

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:

```

```

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`

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 77.8% 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

4299d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c023eaab6cc1be8c58dc45e3dbc3e03997afc0d8f3d05464a7aea4fef6d40cef?d=identicon)[alexander.perov](/maintainers/alexander.perov)

---

Top Contributors

[![gaarf](https://avatars.githubusercontent.com/u/205004?v=4)](https://github.com/gaarf "gaarf (14 commits)")[![clh-code](https://avatars.githubusercontent.com/u/1203334?v=4)](https://github.com/clh-code "clh-code (2 commits)")[![sb15](https://avatars.githubusercontent.com/u/517618?v=4)](https://github.com/sb15 "sb15 (2 commits)")

---

Tags

xml

### Embed Badge

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

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

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k135.8M851](/packages/jms-serializer)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[hassankhan/config

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

97513.5M170](/packages/hassankhan-config)[sabre/xml

sabre/xml is an XML library that you may not hate.

52832.2M131](/packages/sabre-xml)

PHPackages © 2026

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