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

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

baraja-core/xml-to-php-array
============================

XML to PHP array convertor

v2.1.1(3y ago)532.2k11PHPPHP ^8.0

Since Jan 1Pushed 3y ago1 watchersCompare

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

READMEChangelog (8)Dependencies (7)Versions (12)Used By (1)

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

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

Smart tool to convert your XML to PHP array.

This is fork from [gaarf/XML-string-to-PHP-array](https://github.com/gaarf/XML-string-to-PHP-array).

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

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

Use Composer:

```
composer require baraja-core/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

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 96% 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 ~88 days

Recently: every ~145 days

Total

8

Last Release

1345d ago

Major Versions

v1.1.0 → v2.0.02021-02-11

PHP version history (2 changes)v1.0.0PHP &gt;=7.1.0

v2.0.0PHP ^8.0

### Community

Maintainers

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

---

Top Contributors

[![janbarasek](https://avatars.githubusercontent.com/u/4738758?v=4)](https://github.com/janbarasek "janbarasek (24 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")

---

Tags

arraycastingconvertorphpphp-arraysnippetxml

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[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)[meyfa/php-svg

Read, edit, write, and render SVG files with PHP

54613.9M42](/packages/meyfa-php-svg)

PHPackages © 2026

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