Skip to content
Loading the editor only when it is ready

PHP Array to YAML Converter

Turn any PHP return array into clean, well-formatted YAML. Multi-dimensional arrays with => arrow notation become indented YAML structures — perfect for moving from hardcoded PHP configs to YAML files.

Use Cases

  • Laravel Migration — Convert Laravel config arrays to YAML files
  • Config Extraction — Extract PHP array configs into standalone YAML
  • Cross-Platform Config — Make PHP configs accessible to non-PHP tools
  • Documentation — Generate YAML examples from existing PHP configs

Example

Input (PHP array):

<?php
return [
  'app' => [
    'name' => 'MyApp',
    'debug' => true,
    'port' => 8080,
  ],
  'database' => [
    'driver' => 'mysql',
    'host' => 'localhost',
    'credentials' => [
      'user' => 'admin',
      'password' => 'secret',
    ],
  ],
];

Output (YAML):

app:
  name: MyApp
  debug: true
  port: 8080
database:
  driver: mysql
  host: localhost
  credentials:
    user: admin
    password: secret

Understanding PHP Array Syntax

PHP arrays use the => (double arrow) operator to map keys to values. Arrays can be indexed (numeric keys) or associative (string keys), and they support arbitrary nesting depth. This flexibility makes PHP arrays the de facto configuration format in the PHP ecosystem.

Common PHP array patterns:

  • Simple arrays['item1', 'item2', 'item3'] (becomes YAML list)
  • Associative arrays['key' => 'value'] (becomes YAML mapping)
  • Mixed arrays['key' => 'value', 0 => 'item']
  • Multi-dimensional — Nested arrays inside arrays

PHP Array Syntax vs YAML

Feature PHP Array YAML
Key-value separator => (double arrow) : (colon + space)
Nesting Brackets [...] Indentation
String quotes Single or double Optional for simple strings
Booleans true, false (lowercase) true, false, yes, no
Null null null, ~
Integers Unquoted numbers Unquoted numbers
Lists Indexed arrays - prefixed items

Type Handling

The converter preserves PHP type information in the YAML output:

  • Booleanstrue and false are converted to YAML booleans (true/false)
  • Integers — Numeric values without quotes become YAML integers
  • Floats — Decimal numbers are preserved as YAML floats
  • Null — PHP null becomes YAML null
  • Strings — All other values remain strings, quoted if they contain special characters

Use Case: Laravel Config Migration

Laravel applications store configuration in PHP arrays under the config/ directory. Converting these to YAML enables cross-language tooling:

<?php
return [
  'database' => [
    'default' => 'mysql',
    'connections' => [
      'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
      ],
    ],
  ],
];

Output (YAML):

database:
  default: mysql
  connections:
    mysql:
      driver: mysql
      host: 127.0.0.1
      port: 3306
      database: forge
      username: forge
      password: ""
      charset: utf8mb4
      collation: utf8mb4_unicode_ci
      prefix: ""
      strict: true

Multi-Dimensional Arrays

PHP supports deeply nested arrays, which map naturally to YAML's indentation-based hierarchy:

<?php
return [
  'service' => [
    'name' => 'api-gateway',
    'routes' => [
      'users' => [
        'endpoint' => '/api/users',
        'methods' => ['GET', 'POST', 'PUT', 'DELETE'],
        'rate_limit' => [
          'enabled' => true,
          'max_requests' => 100,
          'window_seconds' => 60,
        ],
      ],
    ],
    'middleware' => [
      'auth' => ['jwt', 'oauth'],
      'logging' => true,
    ],
  ],
];

Output (YAML):

service:
  name: api-gateway
  routes:
    users:
      endpoint: /api/users
      methods:
        - GET
        - POST
        - PUT
        - DELETE
      rate_limit:
        enabled: true
        max_requests: 100
        window_seconds: 60
  middleware:
    auth:
      - jwt
      - oauth
    logging: true

Common Pitfalls

  • Unquoted string keys — PHP resolves unquoted string keys as constants; always quote keys
  • Trailing commas — PHP allows trailing commas in arrays; YAML does not use commas for structure
  • Mixed key types — Combining integer and string keys works in PHP but may produce unexpected YAML
  • Variable interpolation — PHP $ variables embedded in values are preserved as-is in YAML output
  • HEREDOC syntax — Not supported; expand heredoc values before converting

How the Conversion Works

  1. Parse — The PHP array syntax is parsed, extracting keys and values recursively
  2. Type map — PHP types (boolean, integer, float, null, string) are mapped to YAML equivalents
  3. Nest — Nested arrays are converted to nested YAML mappings
  4. Serialize — The result is rendered as indented YAML
  5. Format — Output is cleanly formatted with consistent indentation

Related Tools

  • PHP Array to YAML — You are here
  • YAML to PHP Array — Reverse conversion from YAML to PHP return arrays
  • JSON to YAML — Convert JSON objects to YAML
  • INI to YAML — Convert INI config files to YAML
  • YAML Validator — Validate generated YAML output

FAQs

Q: Can the converter handle PHP arrays with mixed key types? A: Yes. String keys and integer keys are both supported. The converter preserves the original key types and converts nested arrays recursively.

Q: Is my PHP array data sent to a server? A: No. All processing happens 100% in your browser. Your data never leaves your computer.

Q: Does it support short array syntax ([...])? A: Yes. Both array(...) and [...] syntax are supported.

Q: What about PHP constant references? A: Constants are not evaluated. They are treated as string literals in the YAML output.

Q: Can it handle very large arrays? A: Yes. Performance scales well with array size, but extremely large arrays (10,000+ elements) may take a few seconds.

Q: How are empty arrays handled? A: Empty arrays are converted to empty YAML mappings ({}) or empty lists depending on context.

Related Tools

Try these complementary developer tools:

Popular Developer Tools

Most-used tools on LangStop