XML to PHP Converter

Transform XML data into PHP arrays and objects for web development and data processing

XML Input

PHP Output

About XML to PHP Converter

Convert XML data to PHP arrays and objects with automatic type detection, proper string escaping, and multiple output formats. Perfect for web development, API integration, and data processing.

Key Features

  • Multiple Output Formats: Associative arrays, indexed arrays, or objects
  • Automatic Type Detection: Detects numbers, booleans, and strings
  • String Escaping: Properly escapes special characters in strings
  • Null Handling: Converts empty/null values to PHP null
  • Pretty Print: Optional formatted output with indentation
  • PHP Tags: Optional <?php opening tags
  • File Download: Save as .php file for direct use

How to Use

  1. Input XML Data: Paste your XML data or upload an .xml file
  2. Select Format: Choose between associative arrays, indexed arrays, or objects
  3. Configure Options: Toggle PHP tags and pretty print formatting
  4. Copy or Download: Use the Copy or Download button to save your PHP code

Output Format Options

  • Associative Arrays: Arrays with column names as keys (most common)
  • Indexed Arrays: Simple numeric-indexed arrays
  • Objects: PHP objects with properties for each column

Example Conversion

XML Input:

<?xml version="1.0" encoding="UTF-8"?>
<products>
  <product>
    <id>1</id>
    <name>Laptop</name>
    <price>999.99</price>
    <in_stock>true</in_stock>
  </product>
  <product>
    <id>2</id>
    <name>Mouse</name>
    <price>24.99</price>
    <in_stock>true</in_stock>
  </product>
</products>

PHP Output (Associative Arrays):

<?php

$data = [
    [
        'id' => 1,
        'name' => 'Laptop',
        'price' => 999.99,
        'in_stock' => true,
    ],
    [
        'id' => 2,
        'name' => 'Mouse',
        'price' => 24.99,
        'in_stock' => true,
    ]
];

Common Use Cases

  • Web Development: Import XML data into PHP applications
  • API Integration: Convert XML API responses to PHP arrays
  • Database Seeding: Create seed data for Laravel, Symfony, etc.
  • Configuration Files: Convert XML config to PHP arrays
  • Data Migration: Migrate XML data to PHP-based systems
  • Testing: Generate test fixtures from XML data
  • CMS Development: Import content from XML to PHP CMS

Type Detection

The converter automatically determines the appropriate PHP type:

  • Numbers: Integer or float values (e.g., 42, 3.14)
  • Booleans: true/false values
  • Strings: Text values with proper escaping
  • Null: Empty, null, or NULL values

Working with the Output

Associative Arrays:

foreach ($data as $row) {
    echo $row['name'] . ': $' . $row['price'];
}

Indexed Arrays:

foreach ($data as $row) {
    echo $row[1] . ': $' . $row[2];
}

Objects:

foreach ($data as $obj) {
    echo $obj->name . ': $' . $obj->price;
}

PHP Frameworks

The generated code works with all PHP frameworks:

  • Laravel: Use for database seeding, factories, and migrations
  • Symfony: Import data for fixtures and configuration
  • CodeIgniter: Load data into models and controllers
  • WordPress: Import posts, custom fields, and metadata
  • Drupal: Create content and configuration arrays
  • Yii: Generate fixtures and test data

XML Structure Requirements

  • Root Element: Single root element containing row elements
  • Row Elements: Direct children of root represent array items
  • Column Elements: Children of row elements represent fields
  • Consistent Structure: All rows should have the same column structure

String Escaping

The converter properly escapes PHP special characters:

  • Backslash (\\): Escaped as \\\\
  • Single Quote ('): Escaped as \\'
  • Newline (\\n): Escaped as \\n
  • Carriage Return (\\r): Escaped as \\r
  • Tab (\\t): Escaped as \\t

Privacy & Security

All conversions happen locally in your browser. Your XML data is never uploaded to any server, ensuring complete privacy and security.