PHPackages                             fillup/array2xml - 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. fillup/array2xml

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

fillup/array2xml
================

Library for converting arrays to XML

0.5.1(9y ago)1232.0k↓18.2%4[1 issues](https://github.com/fillup/array2xml/issues)6MITPHP

Since Jun 19Pushed 9y agoCompare

[ Source](https://github.com/fillup/array2xml)[ Packagist](https://packagist.org/packages/fillup/array2xml)[ RSS](/packages/fillup-array2xml/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (1)Versions (10)Used By (6)

array2xml
=========

[](#array2xml)

array2xml is a simple library for converting arrays to XML

Why?
----

[](#why)

There are several libraries for converting arrays to XML, but they all require special syntax for annotating schema details for things like what to send array items as, attributes, and namespaces.

I was looking for a solution that would allow consumers of other libraries to provide an array without needing to know those xml schema details.

How it works
------------

[](#how-it-works)

This library separates the array data from schema data so it is ideal for use in libraries where schema details are needed but you don't want to put that on users of your library. When instantiating the `fillup\A2X` class you pass the data array as the first parameter and optionally provide a second array parameter with schema details.

This library supports serializing associative arrays, normal arrays, adding attributes, and namespaces.

The schema array is a simple format of an associative array where the key is the path/position in the array and the value is an array with schema details.

### Associative arrays

[](#associative-arrays)

Associative arrays are the easiest thing to serialize to XML because the format `['key' => 'value']` very naturally maps to `value`.

### Non-associative arrays

[](#non-associative-arrays)

In PHP we represent normal arrays something like `['item1', 'item2', 'item3']`, but when serializing to XML this is a challenge because each element must be wrapped with a tag. This can be done by using the `sendItemsAs`element in the schema for a given position. See the example below where the contacts element in the array is an array of associative arrays. The scheme defines to send each as `contact`.

A2X also recognizes simple forms of plurals, so if the array data element has a name of `contacts` and you do not specify what it's items should be sent as it will strip the trailing `s` and send each as `contact`.

### Attributes

[](#attributes)

If you need to use attributes in your xml, like `name@domain.com` you can do so by defining the attributes array in the schema for the position in the XML that needs attributes. The values of the `attributes` array in the schema relate to what child elements should be serialized as attributes. This makes it very natural in the original array to just say:

```
[
    'contact' => [
        'type' => 'email',
        'value' => 'name@domain.com',
    ]
]
```

and in the schema provide:

```
[
    '/path/to/contact' => [
        'attributes' => [
            'type'
        ]
    ]
]
```

See the example below for how `/person` and `/person/contacts/contact` have attributes.

### Namespaces

[](#namespaces)

If you need to use namespaces in your XML there are two places to define them. First you must provide the actual namespace definitions, that is the map from namespace prefix to URI. These are provided in the specal `@namespaces`element of the schema array. Second, for any given position in the schema array you can specify a `namespace`attribute with a single string value that should map to one of the prefixes defined in `@attributes`. See example below for how `ns1` and `ns2` are defined in `@attributes` and then used for positions `/person/contacts` and `/person/contacts/contact`.

If you want to have all elements at and under a specific position to have the same namespace you can use the `childNamespace` attribute in the schema. This will apply the given namespace to all elements below the given position.

### List elements without a parent wrapping tag

[](#list-elements-without-a-parent-wrapping-tag)

If you need to generate a list of elements without a wrapping parent tag, the schema setting for `includeWrappingTag`may be set to `false`. By default it is considered `true` if not present. This is useful in the following example where you have a list of `children` but want them each listed as a `child` element instead of as ``.

```

    Daddio

        Older Brother

        Little Sister

```

Usage
-----

[](#usage)

```

        first
        last

        123 Somewhere

        Anytown
        AA
        USA

    40

            user@domain.com

            11235551234

```

A2X does not currently support pretty printing the xml as is displayed here, but I show it formatted for easier reading.

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

[](#contributing)

Contributions are welcome as either issues or even better pull requests. If you like this library and use it, let me know, I'd love to know if others are benefiting from it as well. phillip dot shipley at gmail.

License
-------

[](#license)

The MIT License (MIT)

Copyright (c) 2016 Phillip Shipley

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity34

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~16 days

Recently: every ~26 days

Total

8

Last Release

3539d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a5e188393958c4505bb06d9e79ed99a47d1cd42551142f02ef364d8268449ef8?d=identicon)[fillup](/maintainers/fillup)

---

Top Contributors

[![fillup](https://avatars.githubusercontent.com/u/556105?v=4)](https://github.com/fillup "fillup (11 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/fillup-array2xml/health.svg)

```
[![Health](https://phpackages.com/badges/fillup-array2xml/health.svg)](https://phpackages.com/packages/fillup-array2xml)
```

###  Alternatives

[mck89/peast

Peast is PHP library that generates AST for JavaScript code

19037.7M41](/packages/mck89-peast)[sauladam/shipment-tracker

Parses tracking information for several carriers, like UPS, USPS, DHL and GLS by simply scraping the data. No need for any kind of API access.

9642.0k](/packages/sauladam-shipment-tracker)[json-mapper/laravel-package

The JsonMapper package for Laravel

25188.9k3](/packages/json-mapper-laravel-package)[moonshine/layouts-field

Field for repeating groups of fields for MoonShine

107.9k](/packages/moonshine-layouts-field)[tcds-io/php-jackson

A lightweight, flexible object serializer for PHP, inspired by FasterXML/jackson

112.9k10](/packages/tcds-io-php-jackson)

PHPackages © 2026

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