PHP Array to XML Converter

Parses a PHP array literal, modern [...] short-array syntax or the older array(...) form, into an equivalent value, then converts it to XML the same way this category's other structured-data-to-XML converters do. The reverse of the XML to PHP Array Converter. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-23

Overview

Introduction

The XML to PHP Array Converter in this category turns XML into a ready-to-paste PHP array literal, but going the other direction, taking a PHP array you already have and turning it back into XML, previously meant hand-transcribing it.

This tool closes that loop: paste a PHP array literal and get back a well-formed XML document.

What Is PHP Array to XML Converter?

A hand-rolled parser reads a PHP array literal (short [...] syntax or the older array(...) form) into an equivalent JS value, following the same parsing engine as the JSON category's PHP Array to JSON Converter.

That value is then converted to XML the same way every other structured-data-to-XML converter in this category works: it's handed to the shared JSON-to-XML conversion step with a fixed root element name.

How PHP Array to XML Converter Works

The parser strips an optional leading <?php tag and $variable = ... ; assignment wrapper, then recursively parses the array body: quoted strings (single or double), 'key' => value pairs, bare positional values, nested arrays, numbers, true/false, and null.

Once parsed, an array is classified as a PHP list (its keys form the contiguous sequence 0, 1, 2, ...) or an associative array; the result is JSON-stringified and passed through this category's shared JSON to XML conversion, wrapping bare top-level lists or scalars under a single item element first.

When To Use PHP Array to XML Converter

Use it when you have a PHP array literal, from a config file, a var_export() dump, or hand-written test fixture data, and need it as XML for a legacy system or config loader.

It's also useful for round-tripping through the XML to PHP Array Converter to sanity-check that a hand-edited PHP array still represents the structure you expect.

Features

Advantages

  • Accepts both modern short-array syntax and the older array(...) form in the same input.
  • Accepts the exact <?php $data = [...]; wrapper the XML to PHP Array Converter produces, so the two tools round-trip directly.
  • Runs entirely client-side, no PHP interpreter involved.
  • Reports the exact line and column of a syntax error rather than failing silently.

Limitations

  • This is a hand-written parser for literal array syntax, not a full PHP interpreter: it can't evaluate expressions, constants, function calls, or variables used as array values.
  • PHP arrays have no XML-tag-name restrictions, so a string key that isn't a valid XML tag name (spaces, a leading digit) falls back to a generic item tag, the same limitation the JSON to XML Converter has.
  • PHP scalar types (int vs numeric string, for example) aren't preserved distinctly in XML text content, which is always plain text.

Examples

Converting a nested associative array

Input

[
    'user' => [
        '@id' => '1',
        'name' => 'Ada',
    ],
]

Output

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <user>
    <item>1</item>
    <name>Ada</name>
  </user>
</root>

The associative array becomes nested elements under the fixed root element; the @id key isn't a valid XML tag name on its own so it falls back to a generic item tag.

Best Practices & Notes

Best Practices

  • Paste the exact output of the XML to PHP Array Converter (including the <?php wrapper) if you're round-tripping, it's stripped automatically.
  • Keep associative array keys as valid XML identifiers (letters, digits, underscore, hyphen, no leading digit or symbol) if you need them preserved as tag names.
  • Check the error message's line and column if parsing fails, most failures are an unterminated string or a missing comma.

Developer Notes

The parsing engine (single-quoted/double-quoted strings, array(...) and [...] bodies, 'key' => value entries, true/false/null/number literals) is duplicated locally from the JSON category's php-array-to-json.ts rather than imported across the category boundary, matching this codebase's convention of category-local lib files. Serialization reuses the shared jsonToXml helper the same way csv-to-xml-converter.ts, ini-to-xml-converter.ts, and toml-to-xml-converter.ts already do.

PHP Array to XML Converter Use Cases

  • Converting a hand-written PHP config array into XML for a legacy loader
  • Turning a var_export()-style PHP array dump into XML for another tool to consume
  • Round-tripping through the XML to PHP Array Converter to verify a structure survives both directions
  • Generating XML test fixtures from PHP array literals already in a codebase

Common Mistakes

  • Using PHP constants, function calls, or variables as array values, only literal strings, numbers, true/false/null, and nested arrays parse.
  • Forgetting a trailing comma isn't required but a missing comma between entries is a parse error.
  • Expecting original PHP scalar types (int vs string) to survive into the XML text content distinctly, they don't.

Tips

  • Use XML to PHP Array Converter first to see what shape of PHP array literal this tool expects.
  • Run the output through XML Prettifier if you need a different indent width.
  • Pair with XML Validator afterward to confirm the generated document is well-formed.

References

Frequently Asked Questions