XML to PHP Array Converter

Converts XML to JSON, then renders that value as a PHP array literal ([...] short-array syntax) wrapped in a full <?php file, ready to paste directly into PHP code. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-23

Overview

Introduction

Pasting XML data into a PHP script usually means manually rewriting it as an array literal.

This tool generates that literal directly.

What Is XML to PHP Array Converter?

A code-generation tool that converts XML to JSON.

It then recursively renders that value as an indented PHP array literal wrapped in a complete <?php file.

How XML to PHP Array Converter Works

XML is converted to JSON the same way the XML to JSON Converter does it.

The parsed value is then walked recursively: objects and arrays become nested [...] array literals with 'key' => value entries, and scalars become PHP literals (numbers, true/false, null, or single-quoted strings).

When To Use XML to PHP Array Converter

Use it when you need XML data available as a native PHP array for a script, test fixture, or config file, without hand-transcribing it.

It's also useful for generating a PHP constant or config array straight from an XML data export.

Features

Advantages

  • Produces ready-to-paste, properly indented PHP.
  • Handles arbitrary nesting and arrays automatically.
  • Runs entirely client-side.

Limitations

  • Every value becomes a PHP scalar or array, there's no attempt to infer richer PHP types.
  • The round trip through PHP Array to XML Converter isn't lossless: XML text content that started as a number or boolean comes back as a plain PHP string unless you cast it yourself.

Examples

Converting a small record

Input

<user id="1"><name>Ada</name></user>

Output

<?php

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

The XML structure becomes a nested PHP array assigned to $data.

Best Practices & Notes

Best Practices

  • Rename the $data variable in the output to whatever fits your script before using it.
  • Use XML to JSON Converter first if you want to double-check the structure before generating PHP from it.
  • Review the @-prefixed attribute keys, some PHP codebases prefer renaming them before use.

Developer Notes

Rendering is a straightforward recursive function with no PHP-specific dependency, indentation is 4 spaces per level, matching common PHP style conventions (PSR-12).

XML to PHP Array Converter Use Cases

  • Turning an XML fixture into a native PHP array for a test
  • Pasting XML config data directly into PHP code
  • Generating a PHP constant or config array from an XML data export

Common Mistakes

  • Forgetting the leading <?php tag is part of the output, meant to be a complete, standalone file.
  • Assuming a round trip through PHP Array to XML Converter restores the exact original XML: nesting and @-prefixed attribute keys survive, but original scalar types (numbers, booleans) don't, since PHP array values become plain XML text either way.

Tips

  • Use XML to JSON Converter first if you want to double-check the structure before generating PHP from it.
  • Check the FAQ above if the output doesn't look like what you expected.

References

Frequently Asked Questions