PHPackages                             aivec/array-to-xml - 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. aivec/array-to-xml

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

aivec/array-to-xml
==================

Convert an array to xml

2.12.0(6y ago)1183MITPHPPHP &gt;=5.6

Since Mar 17Pushed 6y agoCompare

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

READMEChangelogDependencies (5)Versions (28)Used By (0)

Convert an array to xml
=======================

[](#convert-an-array-to-xml)

[![Latest Version](https://camo.githubusercontent.com/90d99372be6fc674e9deca47ddcf03f51f22f5dc5c4323ef8d51ddabe9325ddd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f7370617469652f61727261792d746f2d786d6c2e7376673f7374796c653d666c61742d737175617265)](https://github.com/spatie/array-to-xml/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/ee4af2979091be84acb0938f91ccc6d8f3e8722db1d22304336456740920dc9f/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7370617469652f61727261792d746f2d786d6c2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/spatie/array-to-xml)[![Quality Score](https://camo.githubusercontent.com/5ef15a5d83bfae43aeab789594a2728d6520837a5add26c958e07f6c67482f15/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7370617469652f61727261792d746f2d786d6c2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/spatie/array-to-xml)[![StyleCI](https://camo.githubusercontent.com/0c8f3a7e1b653dc9b659a826a19405c5377f43dc0ecb76f43cd5cfed500d04c6/68747470733a2f2f7374796c6563692e696f2f7265706f732f33323338383734372f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/32388747)[![Total Downloads](https://camo.githubusercontent.com/4f3ea058d8eae1a18c4c592287cda0a1b80b702d183c8c5090b4f8da590a6a02/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f61727261792d746f2d786d6c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/array-to-xml)

This package provides a very simple class to convert an array to an xml string.

PHP 5.6 Compatible
------------------

[](#php-56-compatible)

This is a fork of  simply for compatibility with older versions of PHP.

Install
-------

[](#install)

You can install this package via composer.

```
composer require aivec/array-to-xml
```

Usage
-----

[](#usage)

```
use aivec\ArrayToXml\ArrayToXml;
...
$array = [
    'Good guy' => [
        'name' => 'Luke Skywalker',
        'weapon' => 'Lightsaber'
    ],
    'Bad guy' => [
        'name' => 'Sauron',
        'weapon' => 'Evil Eye'
    ]
];

$result = ArrayToXml::convert($array);
```

After running this piece of code `$result` will contain:

```

        Luke Skywalker
        Lightsaber

        Sauron
        Evil Eye

```

### Setting the name of the root element

[](#setting-the-name-of-the-root-element)

Optionally you can set the name of the rootElement by passing it as the second argument. If you don't specify this argument (or set it to an empty string) "root" will be used.

```
$result = ArrayToXml::convert($array, 'customrootname');

```

### Handling key names

[](#handling-key-names)

By default all spaces in the key names of your array will be converted to underscores. If you want to opt out of this behaviour you can set the third argument to false. We'll leave all keynames alone.

```
$result = ArrayToXml::convert($array, 'customrootname', false);

```

### Adding attributes

[](#adding-attributes)

You can use a key named `_attributes` to add attributes to a node, and `_value` to specify the value.

```
$array = [
    'Good guy' => [
        '_attributes' => ['attr1' => 'value'],
        'name' => 'Luke Skywalker',
        'weapon' => 'Lightsaber'
    ],
    'Bad guy' => [
        'name' => 'Sauron',
        'weapon' => 'Evil Eye'
    ],
    'The survivor' => [
        '_attributes' => ['house'=>'Hogwarts'],
        '_value' => 'Harry Potter'
    ]
];

$result = ArrayToXml::convert($array);
```

This code will result in:

```

        Luke Skywalker
        Lightsaber

        Sauron
        Evil Eye

        Harry Potter

```

### Using reserved characters

[](#using-reserved-characters)

It is also possible to wrap the value of a node into a CDATA section. This allows you to use reserved characters.

```
$array = [
    'Good guy' => [
        'name' => [
            '_cdata' => 'Luke Skywalker'
        ],
        'weapon' => 'Lightsaber'
    ],
    'Bad guy' => [
        'name' => 'Sauron',
        'weapon' => 'Evil Eye'
    ]
];

$result = ArrayToXml::convert($array);
```

This code will result in:

```

        Luke Skywalker]]>
        Lightsaber

        &lt;h1&gt;Sauron&lt;/h1&gt;
        Evil Eye

```

If your input contains something that cannot be parsed a `DOMException` will be thrown.

### Adding attributes to the root element

[](#adding-attributes-to-the-root-element)

To add attributes to the root element provide an array with an `_attributes` key as the second argument. The root element name can then be set using the `rootElementName` key.

```
$result = ArrayToXml::convert($array, [
    'rootElementName' => 'helloyouluckypeople',
    '_attributes' => [
        'xmlns' => 'https://github.com/aivec/array-to-xml',
    ],
], true, 'UTF-8');
```

### Using a multi-dimensional array

[](#using-a-multi-dimensional-array)

Use a multi-dimensional array to create a collection of elements.

```
$array = [
    'Good guys' => [
        'Guy' => [
            ['name' => 'Luke Skywalker', 'weapon' => 'Lightsaber'],
            ['name' => 'Captain America', 'weapon' => 'Shield'],
        ],
    ],
    'Bad guys' => [
        'Guy' => [
            ['name' => 'Sauron', 'weapon' => 'Evil Eye'],
            ['name' => 'Darth Vader', 'weapon' => 'Lightsaber'],
        ],
    ],
];
```

This will result in:

```

            Luke Skywalker
            Lightsaber

            Captain America
            Shield

            Sauron
            Evil Eye

            Darth Vader
            Lightsaber

```

### Handling numeric keys

[](#handling-numeric-keys)

The package can also can handle numeric keys:

```
$array = [
    100 => [
        'name' => 'Vladimir',
        'nickname' => 'greeflas',
    ],
    200 => [
        'name' => 'Marina',
        'nickname' => 'estacet',
    ],
];

$result = ArrayToXml::convert(['__numeric' => $array]);
```

This will result in:

```

        Vladimir
        greeflas

        Marina
        estacet

```

You can change key prefix with setter method called `setNumericTagNamePrefix()`.

### Setting DOMDocument properties

[](#setting-domdocument-properties)

To set properties of the internal DOMDocument object just pass an array consisting of keys and values. For a full list of valid properties consult .

You can use the constructor to set DOMDocument properties.

```
$result = ArrayToXml::convert(
   $array,
   $rootElement,
   $replaceSpacesByUnderScoresInKeyNames,
   $xmlEncoding,
   $xmlVersion,
   ['formatOutput' => true]
);
```

Alternatively you can use `setDomProperties`

```
$arrayToXml = new ArrayToXml($array);
$arrayToXml->setDomProperties(['formatOutput' => true]);
$result = $arrayToXml->toXml();
```

Testing
-------

[](#testing)

```
vendor/bin/phpunit
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Postcardware
------------

[](#postcardware)

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

Support us
----------

[](#support-us)

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/spatie). All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 68.9% 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 ~69 days

Recently: every ~63 days

Total

27

Last Release

2266d ago

Major Versions

0.0.1 → 1.0.02015-03-17

1.0.3 → 2.0.02015-10-08

PHP version history (7 changes)0.0.1PHP &gt;=5.3.0

2.2.1PHP ^5.4|^7.0

2.5.0PHP ^5.6|^7.0

2.5.2PHP ^7.0

2.8.1PHP ^7.1

2.11.0PHP ^7.2

2.12.0PHP &gt;=5.6

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/49274188?v=4)[Aivec LLC.](/maintainers/Aivec)[@aivec](https://github.com/aivec)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (84 commits)")[![elyotechgit](https://avatars.githubusercontent.com/u/251508?v=4)](https://github.com/elyotechgit "elyotechgit (4 commits)")[![willemwollebrants](https://avatars.githubusercontent.com/u/916958?v=4)](https://github.com/willemwollebrants "willemwollebrants (2 commits)")[![andreybolonin](https://avatars.githubusercontent.com/u/2576509?v=4)](https://github.com/andreybolonin "andreybolonin (2 commits)")[![crynobone](https://avatars.githubusercontent.com/u/172966?v=4)](https://github.com/crynobone "crynobone (2 commits)")[![dmaksimov](https://avatars.githubusercontent.com/u/497369?v=4)](https://github.com/dmaksimov "dmaksimov (2 commits)")[![Gemorroj](https://avatars.githubusercontent.com/u/885731?v=4)](https://github.com/Gemorroj "Gemorroj (2 commits)")[![akoepcke](https://avatars.githubusercontent.com/u/5311185?v=4)](https://github.com/akoepcke "akoepcke (2 commits)")[![shimabox](https://avatars.githubusercontent.com/u/2285196?v=4)](https://github.com/shimabox "shimabox (1 commits)")[![daniel-zahariev](https://avatars.githubusercontent.com/u/263063?v=4)](https://github.com/daniel-zahariev "daniel-zahariev (1 commits)")[![vdbelt](https://avatars.githubusercontent.com/u/11087503?v=4)](https://github.com/vdbelt "vdbelt (1 commits)")[![dmitry-ivanov](https://avatars.githubusercontent.com/u/1286821?v=4)](https://github.com/dmitry-ivanov "dmitry-ivanov (1 commits)")[![benjamindoe](https://avatars.githubusercontent.com/u/15779219?v=4)](https://github.com/benjamindoe "benjamindoe (1 commits)")[![EvanShaw](https://avatars.githubusercontent.com/u/6827717?v=4)](https://github.com/EvanShaw "EvanShaw (1 commits)")[![amosmos](https://avatars.githubusercontent.com/u/2318695?v=4)](https://github.com/amosmos "amosmos (1 commits)")[![vyuldashev](https://avatars.githubusercontent.com/u/1809081?v=4)](https://github.com/vyuldashev "vyuldashev (1 commits)")[![greeflas](https://avatars.githubusercontent.com/u/17636915?v=4)](https://github.com/greeflas "greeflas (1 commits)")[![iLexN](https://avatars.githubusercontent.com/u/4638751?v=4)](https://github.com/iLexN "iLexN (1 commits)")[![JoeBengalen](https://avatars.githubusercontent.com/u/11173689?v=4)](https://github.com/JoeBengalen "JoeBengalen (1 commits)")[![kamrava](https://avatars.githubusercontent.com/u/5755992?v=4)](https://github.com/kamrava "kamrava (1 commits)")

---

Tags

xmlarrayconvert

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[openlss/lib-array2xml

Array2XML conversion library credit to lalit.org

31052.5M47](/packages/openlss-lib-array2xml)[clearstream/xml-to-array

Incredibly simple XML to array PHP converter

6736.7k](/packages/clearstream-xml-to-array)[digitickets/lalit

GitHub copy of LaLit's XML2Array and Array2XML

721.3M6](/packages/digitickets-lalit)

PHPackages © 2026

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