Skip to main content
Programming Language Tools

PHP Array to JSON

Convert a PHP array literal into valid JSON.

Paste a PHP array literal below - this tool converts it into valid, formatted JSON.

Going the other direction? Use the JSON to PHP Array converter instead.

What Does This Tool Do?

It parses PHP array syntax (['key' => 'value'] or the older array('key' => 'value') form, including nested arrays) and converts it into equivalent JSON.

Why Use This Tool?

Useful when you have PHP configuration or test data and need the equivalent JSON - for an API response mock, a JavaScript test fixture, or documentation.

How to Use It

  1. Paste a PHP array literal.
  2. Click Convert.
  3. Copy the resulting JSON.

Limitations

This handles array literals containing plain values (strings, numbers, booleans, null, nested arrays) - PHP-specific constructs like object instances, constants, or function calls within the array aren't representable in JSON and won't convert.

Example

array(
    'site' => 'Byteary',
    'free' => true,
    'limits' => ['requests' => 60, 'window' => '1m'],
    'tags' => ['tools', 'developers'],
)

becomes:

{
  "site": "Byteary",
  "free": true,
  "limits": {
    "requests": 60,
    "window": "1m"
  },
  "tags": [
    "tools",
    "developers"
  ]
}

Lists vs Objects

PHP uses one array type for both lists and key-value maps; JSON has two. An array with keys 0, 1, 2... in order becomes a JSON array; any other keys produce a JSON object. This is the same rule PHP's json_encode() follows.

Where It Helps

  • Building mock API responses from Laravel config or test data.
  • Sharing PHP settings with a JavaScript front end.
  • Documenting a configuration format in JSON.

In Your Own Code

echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

Those flags give readable output, keep / unescaped in URLs, and keep non-English text as-is.

Convert back with JSON to PHP Array, and format the JSON with the JSON Formatter.

Frequently Asked Questions

Both short [ ] and long array( ) syntax, with string or numeric keys, nested arrays, strings, numbers, true/false and null.

A PHP array with string keys, or non-sequential numeric keys, has no JSON array equivalent, so it becomes an object. A list with keys 0, 1, 2... becomes a JSON array.

No. Things like new DateTime() or PHP_EOL need PHP to evaluate them. Replace them with literal values first.
Share this tool: