YAML to PHP Array Converter

Paste YAML and get back a PHP array literal using modern `[...]` and `=>` syntax, with strings single-quoted and escaped the way `var_export()` would format them, ready to paste straight into a PHP config file. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-22

Overview

Introduction

PHP projects, especially Laravel-style config files, are usually written as plain array literals, not YAML. When the source of truth for a piece of configuration is YAML (a shared CI file, a Kubernetes ConfigMap, a docs example), converting it into that literal syntax by hand is tedious and error-prone, especially around string escaping.

This tool parses YAML and emits the equivalent PHP array literal directly, ready to paste into a `.php` file.

What Is YAML to PHP Array Converter?

A YAML-to-PHP converter walks a parsed YAML document and renders each mapping as a PHP associative array (`'key' => value`) and each sequence as a plain indexed array, recursively, down to the scalar leaves.

Scalars are formatted the way PHP's `var_export()` would print them: numbers as bare digits, booleans and null as lowercase keywords, and strings single-quoted with the two characters that matter inside single quotes (`\` and `'`) escaped.

How YAML to PHP Array Converter Works

The input is parsed with a full YAML 1.2 parser into a plain JS value, then recursively rendered as PHP source text. Objects become `[ 'key' => value, ... ]` blocks, arrays become `[ value, ... ]` blocks with no keys, and each nesting level adds one more level of four-space indentation.

String escaping only touches backslashes and single quotes, matching single-quoted PHP string literal rules exactly, so no other characters are altered or double-escaped.

When To Use YAML to PHP Array Converter

Use it when migrating a YAML config into a Laravel, Symfony, or plain PHP config file that expects a native array.

It's also handy for quickly checking what a piece of YAML would look like as PHP during a code review or a migration script.

Features

Advantages

  • Produces syntactically correct PHP with proper escaping, no manual quote-fixing needed.
  • Uses PHP's modern short array syntax (`[...]`) rather than the older `array(...)` form.
  • Preserves the full nested structure, including mixed arrays and mappings at any depth.
  • Runs entirely client-side, so configuration values never leave your browser.

Limitations

  • YAML-specific features with no direct PHP scalar equivalent (anchors, merge keys, custom tags) are resolved to their final values before conversion, the anchor/alias relationship itself isn't preserved in the PHP output.
  • Non-finite numbers (should any arise from unusual YAML input) are rendered as `null`, since PHP array literals can't contain `NAN` or `INF` as bare tokens.
  • The output is a single array expression; it does not wrap itself in `<?php ... ?>` tags or a `return` statement, since the right wrapper depends on how you intend to use the file.

Examples

A simple config mapping

Input

name: api
replicas: 3

Output

[
    'name' => 'api',
    'replicas' => 3,
]

Each YAML key becomes a PHP associative array entry with proper `=>` syntax.

A YAML list

Input

- admin
- editor

Output

[
    'admin',
    'editor',
]

Sequences become plain PHP arrays relying on implicit numeric indexing.

Best Practices & Notes

Best Practices

  • Wrap the output in `return ...;` when using it as a standalone PHP config file that gets `include`d or `require`d.
  • Run the YAML through the formatter first if it's in flow style, so you can visually compare it to the PHP output while checking the conversion.
  • Double check any keys that look like they were meant to be integers, YAML and PHP both treat unquoted numeric-looking keys differently depending on context.

Developer Notes

Formatting logic is a hand-written recursive renderer, not a call into any PHP runtime, since this all happens in the browser. It intentionally mirrors `var_export()`'s single-quote-string convention rather than PHP's double-quoted strings, since single quotes need no interpolation-awareness and are the safer default for arbitrary data.

YAML to PHP Array Converter Use Cases

  • Migrating a YAML-based config file into a PHP framework's native array config format
  • Generating PHP fixture data from a YAML source used elsewhere in a project
  • Quickly previewing what a piece of YAML would look like as a PHP array during code review
  • Converting Kubernetes/Helm YAML values into PHP for a deployment tooling script

Common Mistakes

  • Forgetting to wrap the output in a `return` statement when using it as a PHP config file loaded via `include`.
  • Assuming numeric-looking YAML strings that were quoted in the source stay as PHP strings, they do, since quoting is preserved through the parse step.
  • Pasting the output somewhere expecting the older `array(...)` syntax; this tool always emits the short `[...]` form.

Tips

  • If the YAML source has anchors and aliases, resolve and review the parsed structure first (with YAML Editor) so you know exactly what will be duplicated in the PHP output.
  • Use Extract Keys from YAML first if you only want a subset of the document converted, then paste just that subset here.
  • For very large documents, consider Truncate YAML first to get a smaller, still-representative sample to convert.

References

Frequently Asked Questions