PHPackages                             selfphp/data-converter - 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. selfphp/data-converter

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

selfphp/data-converter
======================

Flexible data format converter library for PHP

v1.1.0(11mo ago)21MITPHPPHP &gt;=8.1

Since Jun 9Pushed 11mo agoCompare

[ Source](https://github.com/selfphp/data-converter)[ Packagist](https://packagist.org/packages/selfphp/data-converter)[ RSS](/packages/selfphp-data-converter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (5)Used By (0)

selfphp/data-converter
======================

[](#selfphpdata-converter)

> A lightweight and extensible PHP library to convert structured data between formats.
> **Currently supports:** array → XML, XML → array and array ↔ JSON conversion.

---

🚀 Features
----------

[](#-features)

- ✅ Convert associative arrays to XML
- ✅ Convert XML to associative arrays
- ✅ Convert arrays to JSON (with optional flags)
- ✅ Convert JSON strings to associative arrays
- ✅ Custom root element name
- ✅ Optional XML declaration
- ✅ Null → `xsi:nil` conversion
- ✅ Boolean → string normalization (`true` / `false`)
- ✅ Recursive array handling
- ✅ Clean and readable output (pretty print)
- ✅ Invalid characters are automatically removed
- ✅ Minimal and modern code (no dependencies)

---

📦 Installation
--------------

[](#-installation)

```
composer require selfphp/data-converter
```

> ✅ PHP 8.1 or higher required

---

✨ Usage Example
---------------

[](#-usage-example)

### Array to XML

[](#array-to-xml)

```
use Selfphp\DataConverter\Format\ArrayToXmlConverter;

$array = [
    'user' => [
        'name' => 'Alice',
        'active' => true,
        'note' => null
    ]
];

$xml = ArrayToXmlConverter::convertArray(
    $array,
    rootElement: 'response',
    addXmlDeclaration: true,
    convertNullToXsiNil: true,
    convertBoolToString: true
);

echo $xml;
```

### XML to Array

[](#xml-to-array)

```
use Selfphp\DataConverter\Format\XmlToArrayConverter;

$xml =  'true',
//     '#text' => 'Alice'
// ]
```

### Array to JSON

[](#array-to-json)

```
use Selfphp\DataConverter\Format\ArrayToJsonConverter;

$data = ['url' => 'https://example.com'];

$converter = (new ArrayToJsonConverter())
    ->withFlags(JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);

echo $converter->convert($data);
```

### JSON to Array

[](#json-to-array)

```
use Selfphp\DataConverter\Format\JsonToArrayConverter;

$json = '{"name":"Alice","active":true}';

$converter = new JsonToArrayConverter();
$array = $converter->convert($json);
```

---

🧪 Tests
-------

[](#-tests)

Run all PHPUnit tests:

```
vendor/bin/phpunit --testdox
```

---

📁 Project Structure
-------------------

[](#-project-structure)

```
src/
└── Format/
    ├── ArrayToXmlConverter.php
    ├── XmlToArrayConverter.php
    ├── ArrayToJsonConverter.php
    └── JsonToArrayConverter.php

tests/
└── Format/
    ├── ArrayToXmlConverterTest.php
    ├── XmlToArrayConverterTest.php
    ├── ArrayToJsonConverterTest.php
    └── JsonToArrayConverterTest.php

```

---

⚠️ Limitations and Edge Cases
-----------------------------

[](#️-limitations-and-edge-cases)

While the XmlToArrayConverter is suitable for most real-world use cases, there are a few edge cases and known limitations to be aware of:

### ❌ Mixed Content

[](#-mixed-content)

XML nodes with both text and child elements (mixed content) are not fully preserved. For example:

```
This is bold and normal text.
```

Would result in:

```
['b' => 'bold'] // Text parts around  are not preserved
```

### ⚠️ Empty Elements

[](#️-empty-elements)

Empty elements like `` are interpreted as empty strings, not as `null` or empty arrays. If needed, you can post-process the result accordingly.

### ⚠️ All values are strings

[](#️-all-values-are-strings)

XML data types (numbers, booleans) are not automatically casted. For example:

```
true
```

Becomes:

```
['active' => 'true'] // not boolean true
```

### ❌ XML Namespaces

[](#-xml-namespaces)

Namespaces (e.g. `xmlns` or prefixed elements) are ignored and stripped automatically. Support for namespaces may be added in a future release.

---

If you encounter any of these scenarios in real-world data, feel free to contribute or open an issue 🙌

---

🛠 Planned
---------

[](#-planned)

- XML → Array
- JSON ↔ Array
- JSON ↔ XML
- CLI support (`php convert input.json`)
- Stream support

---

📄 License
---------

[](#-license)

MIT License – free for personal and commercial use.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance51

Moderate activity, may be stable

Popularity4

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

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

3

Last Release

343d ago

Major Versions

v0.9.0 → v1.0.02025-06-09

### Community

Maintainers

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

---

Top Contributors

[![DamirEnseleit](https://avatars.githubusercontent.com/u/206047444?v=4)](https://github.com/DamirEnseleit "DamirEnseleit (5 commits)")[![selfphp](https://avatars.githubusercontent.com/u/12782362?v=4)](https://github.com/selfphp "selfphp (1 commits)")

---

Tags

xmlarraydataconverterPHP Libraryarray-to-xmlselfphpdata-converterxml-export

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/selfphp-data-converter/health.svg)

```
[![Health](https://phpackages.com/badges/selfphp-data-converter/health.svg)](https://phpackages.com/packages/selfphp-data-converter)
```

###  Alternatives

[openlss/lib-array2xml

Array2XML conversion library credit to lalit.org

31052.5M47](/packages/openlss-lib-array2xml)[jbzoo/data

An extended version of the ArrayObject object for working with system settings or just for working with data arrays

891.6M23](/packages/jbzoo-data)[digitickets/lalit

GitHub copy of LaLit's XML2Array and Array2XML

721.3M6](/packages/digitickets-lalit)[graze/data-structure

Data collections and containers

12287.4k8](/packages/graze-data-structure)[hi-folks/data-block

Data class for managing nested arrays and JSON data.

1472.2k](/packages/hi-folks-data-block)[selective/transformer

A strictly typed array transformer with dot-access, fluent interface and filters.

3817.8k1](/packages/selective-transformer)

PHPackages © 2026

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