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

Abandoned → [rboonzaijer/php-array-to-xml](/?search=rboonzaijer%2Fphp-array-to-xml)Library[Utility &amp; Helpers](/categories/utility)

refactorstudio/php-array-to-xml
===============================

Convert an array to XML with PHP

2.0.5(1y ago)04751[1 issues](https://github.com/rboonzaijer/php-array-to-xml/issues)[1 PRs](https://github.com/rboonzaijer/php-array-to-xml/pulls)MITPHPPHP ^8.0|^7.0CI passing

Since Apr 15Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/rboonzaijer/php-array-to-xml)[ Packagist](https://packagist.org/packages/refactorstudio/php-array-to-xml)[ Docs](https://github.com/rboonzaijer/php-array-to-xml)[ RSS](/packages/refactorstudio-php-array-to-xml/feed)WikiDiscussions main Synced 6d ago

READMEChangelog (9)Dependencies (2)Versions (12)Used By (0)

PhpArrayToXml
=============

[](#phparraytoxml)

Convert an array to XML with PHP

[![](https://camo.githubusercontent.com/8415178e625cc849b9ea1e877e81d2d112708d9eb5ecf7837ec0f1c1e960c252/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f72626f6f6e7a61696a65722f7068702d61727261792d746f2d786d6c2f74657374732e796d6c3f6272616e63683d6d61696e)](https://github.com/rboonzaijer/php-array-to-xml/actions/workflows/tests.yml)[![License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/rboonzaijer/php-array-to-xml/blob/main/LICENSE.md)

 [![](https://camo.githubusercontent.com/0a06578475a4f8afed9182968623458b6d188eb7dde2eadbe757d873c8046251/68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d372e302d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265)](https://github.com/rboonzaijer/php-array-to-xml/actions/workflows/tests.yml)[![](https://camo.githubusercontent.com/4d19563487a6813a84cdb7461597d0fdddb52db29704014bdf77c41682a40a18/68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d372e312d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265)](https://github.com/rboonzaijer/php-array-to-xml/actions/workflows/tests.yml)[![](https://camo.githubusercontent.com/04fab3eeffd41cc36ade8d897bcfc93afe7d3d34677ebb0de8c7310fd5b20d09/68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d372e322d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265)](https://github.com/rboonzaijer/php-array-to-xml/actions/workflows/tests.yml)[![](https://camo.githubusercontent.com/a5dab82ce67e57aba7e837c21fa69aa68de17c984b8368e485799da53a59a3c9/68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d372e332d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265)](https://github.com/rboonzaijer/php-array-to-xml/actions/workflows/tests.yml)[![](https://camo.githubusercontent.com/9f0dc4abccb8f0265049c2f4f7ef603bb6f5c28af28dad4a0b8015de06d44161/68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d372e342d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265)](https://github.com/rboonzaijer/php-array-to-xml/actions/workflows/tests.yml)[![](https://camo.githubusercontent.com/aa4055d101e98cab977be30d61e23a33e81868ae4eb9a98396ab530635eb0568/68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d382e302d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265)](https://github.com/rboonzaijer/php-array-to-xml/actions/workflows/tests.yml)[![](https://camo.githubusercontent.com/e9416836c3b8f85da443380d3c66a621814fb5a27bd23f12da0768cd576b47b9/68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d382e312d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265)](https://github.com/rboonzaijer/php-array-to-xml/actions/workflows/tests.yml)[![](https://camo.githubusercontent.com/4e0975b5c5ab549410bbf69d2c4e6d5ee78e31b5bd6eb84141fc78189412f5aa/68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d382e322d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265)](https://github.com/rboonzaijer/php-array-to-xml/actions/workflows/tests.yml)[![](https://camo.githubusercontent.com/0e0bb2989d263b03ef0440794d0efa7a1529d10b4e248c6f8c4ecce155b7704d/68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d382e332d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265)](https://github.com/rboonzaijer/php-array-to-xml/actions/workflows/tests.yml)[![](https://camo.githubusercontent.com/db9e62b85febb676d741e2f37b37cbe076da5151168d6f7b63dd3de4dabb324b/68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d382e342d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265)](https://github.com/rboonzaijer/php-array-to-xml/actions/workflows/tests.yml)[![](https://camo.githubusercontent.com/05bbd6203812d9c0aa1a0932ec4e9f1773a1c0c84ab001abdc8652ba8ca88ed7/68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d382e352d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265)](https://github.com/rboonzaijer/php-array-to-xml/actions/workflows/tests.yml)

Install
-------

[](#install)

```
composer require rboonzaijer/php-array-to-xml ^2.0

```

Require the vendor files (these already loaded if you are using something like Symfony or Laravel)

```
require __DIR__ . '/vendor/autoload.php';
```

Usage
-----

[](#usage)

Basic example:

```
use RBoonzaijer\PhpArrayToXml\PhpArrayToXml;

$converter = new PhpArrayToXml();

$result = $converter->toXmlString(['title' => 'My Products']);
```

Output:

```

My Products
```

Output Format (Prettify)
------------------------

[](#output-format-prettify)

> `->setFormatOutput(bool $value = false)`

> Alias: `->prettify()` is the same as typing: `->setFormatOutput(true)`

```
$array = [
  'title' => 'My Products',
  'pricing' => 'Pricing'
];
```

Default:

```

My ProductsPricing
```

Usage:

```
$result = $converter->setFormatOutput(true)->toXmlString($array);

// or use the alias:
$result = $converter->prettify()->toXmlString($array);
```

Result:

```

  My Products
  Pricing

```

Custom root name
----------------

[](#custom-root-name)

> `->setCustomRootName(string $value = 'root')`

```
$result = $converter->setCustomRootName('data')->toXmlString();
```

Result:

```

  ...

```

Custom tag name
---------------

[](#custom-tag-name)

> Custom tag names are used when an array has no key names
>
> `->setCustomTagName(string $value = 'node')`

```
$array = [
  'title' => 'My Products',
  'products' => [
    [
      'name' => 'Raspberry Pi 3',
      'price' => 39.99
    ],
    [
      'name' => 'Arduino Uno Rev3',
      'price' => 19.99
    ]
  ]
];
```

Default (prettified):

```

  My Products

      Raspberry Pi 3
      39.99

      Arduino Uno Rev3
      19.99

```

Usage:

```
$xml_string = $converter->setCustomTagName('item')->toXmlString($array);
```

Result (prettified):

```

  My Products

      Raspberry Pi 3
      39.99

      Arduino Uno Rev3
      19.99

```

XML version
-----------

[](#xml-version)

> `->setVersion(string $value = '1.0')`

```
$xml_string = $converter->setVersion('1.1')->toXmlString(['test']);
```

Result (prettified):

```

  test

```

XML encoding
------------

[](#xml-encoding)

> `->setEncoding(string $value = 'UTF-8')`

```
$xml_string = $converter->setEncoding('ISO-8859-1')->toXmlString(['test']);
```

Result (prettified):

```

  test

```

Tag separator
-------------

[](#tag-separator)

> Set the value for the separator that will be used to replace special characters in tag names
>
> `->setSeparator(string $value = '_')`

```
$array = [
  'some of these keys have' => 'My Value 1',
  'spaces in them' => 'My Value 2',
];
```

Default (prettified):

```

  My Value 1
  My Value 2

```

Usage:

```
$xml_string = $converter->setSeparator('-')->toXmlString($array);
```

Result (prettified):

```

  My Value 1
  My Value 2

```

Transform tag names
-------------------

[](#transform-tag-names)

> Transform tag names to uppercase/lowercase
>
> `->setTransformTags(string $value = null)`

```
$array = [
  'This' => [
    'Is' => [
      'an',
      'Example'
    ]
  ]
];
```

Default (prettified):

```

      an
      Example

```

Usage (lowercase):

```
$xml_string = $converter->setTransformTags('lowercase')->toXmlString($array);
```

Result (prettified):

```

      an
      Example

```

Usage (uppercase):

```
$xml_string = $converter->setTransformTags('uppercase')->toXmlString($array);
```

Result (prettified):

```

      an
      Example

```

Usage (uppercase, but with custom tag names, which will not be transformed):

```
$xml_string = $converter
              ->setTransformTags('uppercase')
              ->setCustomRootName('MyRoot')
              ->setCustomTagName('MyCustomTag')
              ->toXmlString($array);
```

Result (prettified):

```

      an
      Example

```

Set numeric tag suffix
----------------------

[](#set-numeric-tag-suffix)

> If this is not null, it appends the numeric array key to the tag name, with the value as separator.
>
> `->setNumericTagSuffix(string $value = null)`

```
$array = [
  'this',
  'is',
  'an'
  [
    'example',
    'using',
    'numeric tag suffix',
  ],
];
```

Default (prettified):

```

  this
  is
  an

    example
    using
    numeric tag suffix

```

Usage:

```
$xml_string = $converter->setNumericTagSuffix('_')->toXmlString($array);
```

Result (prettified):

```

  this
  is
  an

    example
    using
    numeric tag suffix

```

Cast boolean values
-------------------

[](#cast-boolean-values)

> By default boolean values from the array will be cast to the string 'true' or 'false'. You can choose to cast it to any (string) value you like. This method only works on real boolean values, so strings with the value 'true' and 'false' are untouched.
>
> `->setCastBooleanValueTrue(string $value = 'true')`

> `->setCastBooleanValueFalse(string $value = 'false')`

```
$array = [
  'StringTrue' => 'true',
  'StringFalse' => 'false',
  'BooleanTrue' => true,
  'BooleanFalse' => false
];
```

Default (prettified):

```

  true
  false
  true
  false

```

Usage:

```
$xml_string = $converter->setCastBooleanTrue('Yes')->setCastBooleanFalse('No')->toXmlString($array);
```

Result (prettified):

```

  true
  false
  Yes
  No

```

Cast NULL values
----------------

[](#cast-null-values)

> By default null values from the array will have no value in the XML, so the tag looks something like this: ``. You can choose to cast it to any (string) value you like. This method only works on real 'null' values, so strings with the value `'null'` or empty strings `''` are untouched.
>
> `->setCastNullValue(null|string $value = null)`

```
$array = [
  'StringNull' => 'null',
  'StringEmpty' => '',
  'RealNull' => null
];
```

Default (prettified):

```

  null

```

Usage:

```
$xml_string = $converter->setCastNullValue('__NULL__')->setCastBooleanFalse('No')->toXmlString($array);
```

Result (prettified):

```

  null

  __NULL__

```

Development
-----------

[](#development)

- [DEVELOPMENT.md](DEVELOPMENT.md)

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance66

Regular maintenance activity

Popularity13

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 89.1% 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 ~309 days

Recently: every ~238 days

Total

9

Last Release

483d ago

Major Versions

1.1.1 → 2.0.02022-06-11

PHP version history (2 changes)1.0.0PHP ^7.0

2.0.0PHP ^8.0|^7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3bff536018fb2e6d1e58bf34f5fa8f658f56e7b35574ea8a0b904b21ae3cf352?d=identicon)[rboonzaijer](/maintainers/rboonzaijer)

---

Top Contributors

[![rboonzaijer](https://avatars.githubusercontent.com/u/10514742?v=4)](https://github.com/rboonzaijer "rboonzaijer (49 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (4 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (2 commits)")

---

Tags

arrayarray-to-xmlarray-to-xml-phparray-xmlconvertxmlxml-documentxml-filesxml-outputxml-schemaxmlarrayconvertparserarray-to-xmlphp-array-to-xmlarray-to-xml-php

###  Code Quality

TestsPHPUnit

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/refactorstudio-php-array-to-xml/health.svg)](https://phpackages.com/packages/refactorstudio-php-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)
