{ PHP Array to JSON }

// convert php arrays to clean json instantly

Convert PHP arrays and nested structures to valid JSON instantly. Supports associative arrays, indexed arrays, and complex nested data — browser-based and free.

OUTPUT FORMAT
QUICK INSERT
Paste a PHP array expression. You can include or omit $var = and <?php.

Ready to convert

Paste a PHP array and click Convert

HOW TO USE

  1. 01
    Paste Your Array

    Paste any PHP array — with or without $var = or <?php. Indexed, associative, and nested arrays all work.

  2. 02
    Choose Format

    Select "Pretty Print" for human-readable JSON with indentation, or "Minified" for compact single-line output.

  3. 03
    Copy or Download

    Click "Convert to JSON", then copy the result to clipboard or download it as a .json file.

FEATURES

Associative Arrays Indexed Arrays Nested Structures Pretty Print Minified Output Download as .json Unicode Safe Browser-Based

USE CASES

  • 🔧 Convert legacy PHP configs to JSON format
  • 🔧 Migrate database seed data from PHP to JSON
  • 🔧 Prepare API payloads from PHP arrays
  • 🔧 Debug PHP data structures as readable JSON
  • 🔧 Share structured data with non-PHP teams

WHAT IS THIS?

This tool converts PHP array syntax into valid JSON. PHP uses => for key-value pairs and [] or array() notation, while JSON uses : and mandates double-quoted string keys. This converter handles the translation automatically, including type mapping for booleans, null, integers, and floats.

RELATED TOOLS

FREQUENTLY ASKED QUESTIONS

What PHP array syntax is supported?

Both modern bracket syntax ['key' => 'value'] and classic array('key' => 'value') notation are supported. You can include or omit the variable assignment ($data =) and PHP opening tag (<?php).

Does it support nested arrays?

Yes. Deeply nested associative and indexed arrays convert correctly. PHP nested arrays become nested JSON objects or arrays as appropriate depending on their key types.

What is the difference between pretty and minified output?

Pretty Print adds indentation and line breaks for readability, ideal for development and debugging. Minified output removes all unnecessary whitespace to produce a compact representation suited for production use.

Are PHP constants and functions supported?

Basic literal values (strings, integers, floats, booleans, null) are supported. PHP-defined constants like PHP_EOL or expressions using external functions are not evaluated for security reasons.

Is this tool safe to use with sensitive data?

The conversion uses strict security filters to block dangerous PHP functions. That said, we recommend never pasting production credentials or personally identifiable information into any online tool.

How does PHP true, false, and null convert?

PHP true becomes JSON true, false becomes false, and null becomes null. These are standard type equivalents across both languages.

Do integer keys in PHP arrays affect JSON output?

PHP arrays with sequential integer keys (0, 1, 2…) convert to JSON arrays []. Arrays with string keys or non-sequential integer keys become JSON objects {}.

Can I convert the JSON back to PHP?

This tool converts one direction: PHP to JSON. For the reverse, in PHP you can use json_decode($json, true) to get an associative array back from a JSON string.

PHP Array to JSON Converter — Free Online Tool

Converting PHP arrays to JSON is one of the most common tasks in modern web development. Whether you are migrating legacy PHP applications to RESTful APIs, sharing configuration data with JavaScript frontends, or exporting structured data to other services, transforming PHP array syntax into valid JSON is a daily workflow for PHP developers.

Our PHP Array to JSON converter handles the full spectrum of PHP array types — from simple indexed arrays and associative key-value pairs to deeply nested multi-dimensional structures — and produces clean, spec-compliant JSON in seconds, right in your browser.

PHP Arrays vs JSON: Understanding the Difference

PHP and JSON both represent structured data, but with distinct syntax. A PHP associative array uses the => operator and wraps key-value pairs in square brackets or array() calls. JSON uses : for key-value pairs and mandates double-quoted string keys throughout.

For example, the PHP array ['name' => 'Alice', 'age' => 28, 'active' => true] becomes {"name": "Alice", "age": 28, "active": true} in JSON. While this seems straightforward, the conversion involves rules PHP developers must keep in mind: PHP single-quoted strings become double-quoted JSON strings; PHP true, false, and null map directly to their JSON equivalents; and integer-keyed PHP arrays convert to JSON arrays while string-keyed ones become JSON objects.

How This Tool Works

The converter processes PHP array syntax server-side in a tightly controlled environment. Before any evaluation occurs, the input passes through a security filter that scans for dangerous PHP tokens — file system functions, network calls, process execution, encoding tricks, and more are all blocked at the token level. The input is stripped of any leading <?php or variable assignment to isolate just the array expression.

Once evaluated, the resulting PHP data structure is passed to json_encode() with Unicode-safe flags, producing valid JSON. You can choose between pretty-printed output with indentation or minified single-line output.

Common Use Cases

Supported Array Types

The converter handles all standard PHP array forms. Indexed arrays like [1, 2, 3] become JSON arrays [1, 2, 3]. Associative arrays like ['key' => 'value'] become JSON objects {"key": "value"}. Mixed arrays with both string and integer keys, non-sequential integer keys, and deeply nested multi-dimensional structures are all supported. Both modern short syntax [] and the classic array() notation work interchangeably.

Pretty Print vs Minified JSON

The Pretty Print option adds two-space indentation and line breaks, producing JSON that is easy to read, diff in version control, and share with colleagues. Use this during development and debugging. The Minified option strips all whitespace and produces a compact single-line JSON string, the format used in HTTP response bodies and anywhere bytes matter. Minified JSON is typically 10–30% smaller than its pretty-printed equivalent depending on nesting depth.

Tips for Best Results

Paste only the array expression. You can include a leading $variable = or <?php — the tool strips these automatically. Avoid PHP expressions that depend on runtime values like function calls such as date() or env() since these cannot be safely evaluated. Stick to literal values: strings, integers, floats, booleans, and null for reliable results.

When dealing with Unicode characters in string values, the output uses JSON_UNESCAPED_UNICODE to preserve readable characters rather than escaping them to \uXXXX sequences. This keeps the JSON human-readable while remaining fully compliant with the JSON specification.