PHPackages                             susina/xml-to-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. susina/xml-to-array

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

susina/xml-to-array
===================

Simple PHP library to convert an XML string into an array

v1.2(1y ago)11.7k↓33.3%11Apache-2.0PHPPHP &gt;=8.2CI passing

Since Sep 15Pushed 1y ago1 watchersCompare

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

READMEChangelog (4)Dependencies (5)Versions (5)Used By (1)

Xml to Array Converter
======================

[](#xml-to-array-converter)

[![Test Suite](https://github.com/susina/xml-to-array/actions/workflows/test.yml/badge.svg)](https://github.com/susina/xml-to-array/actions/workflows/test.yml/badge.svg)[![Maintainability](https://camo.githubusercontent.com/071f19f9534ed4c36449824b6640afb582d68afc106868da6ba74b86044a0504/68747470733a2f2f716c74792e73682f6261646765732f39333437363966652d613535612d343235632d613837632d6564316164323766653366362f6d61696e7461696e6162696c6974792e737667)](https://qlty.sh/gh/susina/projects/xml-to-array)[![Code Coverage](https://camo.githubusercontent.com/2b6285e43b5bec1ac25e9ab2902f0f32edac700695a408cf9aedc422d4bd7e1f/68747470733a2f2f716c74792e73682f6261646765732f39333437363966652d613535612d343235632d613837632d6564316164323766653366362f746573745f636f7665726167652e737667)](https://qlty.sh/gh/susina/projects/xml-to-array)[![GitHub License](https://camo.githubusercontent.com/b3e7638fc684702cfd7400bbb16ebc1932f4cb7bb0783552e2c44a56427c74d0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f737573696e612f786d6c2d746f2d6172726179)](https://camo.githubusercontent.com/b3e7638fc684702cfd7400bbb16ebc1932f4cb7bb0783552e2c44a56427c74d0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f737573696e612f786d6c2d746f2d6172726179)

Xml to Array is a simple library to convert XML into PHP array.

The library consists in one namespace `Susina\XmlToArray` and two classes:

- `Converter`: to convert an XML string into PHP array
- `FileConverter`: to convert an XML file

Both classes expose the same public api:

- **Susina\\XmlToArray\\Converter**
    - `convert(string $xmlToParse): array` to convert an xml string into an array
    - `convertAndSave(string $xmlToParse, string $filename): void` to convert an xml string to an array and save it into a regular php file.
- **Susina\\XmlToArray\\FileConverter**
    - `convert(string $xmlFile): array` to read an xml file and convert it into an array
    - `convertAndSave(string $xmlFile, string $filename): void` to read an xml file and, convert it into an array and save it into a regular php file.

Installation
------------

[](#installation)

Install the library via [composer](https://getcomposer.org):

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

The library depends on three php extensions, usually installed by default:

- libxml
- simplexml
- dom

and [Symfony Options Resolver](https://symfony.com/doc/current/components/options_resolver.html) component, that'll be install by composer.

Usage
-----

[](#usage)

Use the `convert` method to parse an XML string. Let's get a look at the following example:

```
 [
 *         0 => [
 *             "title"      => "Star Wars",
 *             "starred"    => true,
 *             "percentage" => 32.5
 *         ],
 *         1 => [
 *             "title"      => "The Lord Of The Rings",
 *             "starred"    => false,
 *             "percentage" => 30.7
 *         ]
 *     ]
 * ]
 */
```

Alternatively, you can use the static instantiator:

```

      stream
      /var/log/default.log
      300

';

//mergeAttributes is true by default
$array = Converter::create()->convert($xmlString);

/*
 * $array now contains the following array:
 *
 * [
 *     "logger" => [
 *         "name"  => "defaultLogger",
 *         "type"  => "stream",
 *         "path"  => "/var/log/default.log"
 *         "level" => 300
 *     ]
 * ]
 */
```

In the previous example, you can see that the *name* attribute is "merged" into *logger* array.

When this option is set to false, a *@attribute* array is created:

```
 false])->convert($xmlString);

/*
 * $array now contains the following array:
 *
 * [
 *     "logger" => [
 *         "@attributes" => [
 *             "name" => "defaultLogger"
 *          ],
 *         "type"  => "stream",
 *         "path"  => "/var/log/default.log"
 *         "level" => 300
 *     ]
 * ]
 */
```

### typesAsString

[](#typesasstring)

> Default: **false**

The normal behavior of this library is to preserve all PHP types (boolean, numeric, null etc.). If *typesAsString* option is set to *true* all the values are considered **strings**:

```
 [
 *         0 => [
 *             "title"      => "Star Wars",
 *             "starred"    => true,
 *             "percentage" => 32.5,
 *             "views"      => 589623
 *         ],
 *     ]
 * ]
 */
```

In the previous example, if you set the property to *true*, all values are strings:

```
 [
 *         0 => [
 *             "title" => "Star Wars",
 *         ],
 *         1 => [
 *             "title" => "The Lord Of The Rings",
 *         ],
 *         2 => [
 *             "title" "Spider-Man"
 *         ]
 *     ]
 * ]
 */
```

If you want to keep this tag as the first key of your array, set this option to true:

```
