NPM Package

Convert JSON to PHP Arrays

Seamlessly transform JSON strings into PHP-style associative arrays with customizable formatting. Perfect for API integration and data migration.

Zero Dependencies
TypeScript Ready
Customizable
JavaScript
"keyword">import { jsonToPhp } "keyword">from '@timkit/json-to-php';

// Simple conversion
"keyword">const json = '{"name": "John", "age": 30}';
"keyword">const php = jsonToPhp(json);
// Result: ['name' => 'John', 'age' => 30]

// Nested objects
"keyword">const nested = '{"user": {"name": "Alice", "active": true}}';
"keyword">const beautified = jsonToPhp(nested, { 
  beautify: true 
});

// Output:
// [
//   'user' => [
//     'name' => 'Alice',
//     'active' => true
//   ]
// ]

Try It Live

Convert JSON to PHP arrays instantly with our interactive tool

JSON Input

Beautify
Indent:
Type:

PHP Output

PHP output will appear here...

Get Started in Seconds

Install and start converting JSON to PHP arrays with just a few lines of code

Installation

npm install @timkit/json-to-php

Zero dependencies means a clean, lightweight addition to your project.

Basic Usage

"keyword">import { jsonToPhp } "keyword">from '@timkit/json-to-php';

"keyword">const json = '{"key": "value"}';
"keyword">const php = jsonToPhp(json);

console.log(php);
// ['key' => 'value']

Simple API that handles complex nested structures automatically.

Powerful JSON to PHP Conversion

Everything you need to convert JSON data to PHP arrays with precision and style

Nested Support

Handles deeply nested JSON objects and arrays, converting them to proper PHP associative arrays.

Custom Formatting

Beautify output with configurable indentation, line breaks, and spacing options.

Data Type Handling

Properly converts strings, numbers, booleans, null values, and complex data structures.

Zero Dependencies

Lightweight package with no external dependencies, keeping your bundle size minimal.

Type Safe

Full TypeScript support with proper type definitions for enhanced development experience.

Error Handling

Robust error handling for invalid JSON with clear error messages and edge case support.

Configuration Options

Customize the output format to match your PHP coding standards and preferences

Available Options

Option Type Default Description
beautify boolean false Add line breaks and indentation for readability
indentLevel number 0 Initial indentation level
indentSize number 2 Number of spaces for each indentation level
useTabs boolean false Use tabs instead of spaces for indentation

Default Output

"keyword">const json = '{"name": "John", "details": {"age": 30, "city": "NYC"}}';
"keyword">const php = jsonToPhp(json);

// Result(single line):
['name' => 'John', 'details' => ['age' => 30, 'city' => 'NYC']]

Beautified Output

"keyword">const json = '{"name": "John", "details": {"age": 30, "city": "NYC"}}';
"keyword">const php = jsonToPhp(json, { 
  beautify: true, 
  indentSize: 4 
});

// Result(formatted):
[
    'name' => 'John',
    'details' => [
        'age' => 30,
        'city' => 'NYC'
    ]
]

Real-World Examples

See how @timkit/json-to-php handles various JSON structures and data types

Complex JSON Structures

"keyword">import { jsonToPhp } "keyword">from '@timkit/json-to-php';

"keyword">const complexJson = `{
  "users": [
    {
      "id": 1,
      "name": "Alice Johnson",
      "email": "alice@example.com",
      "profile": {
        "age": 28,
        "location": "San Francisco",
        "preferences": {
          "theme": "dark",
          "notifications": true
        }
      },
      "roles": ["admin", "user"]
    },
    {
      "id": 2,
      "name": "Bob Smith",
      "email": "bob@example.com",
      "profile": {
        "age": 32,
        "location": "New York",
        "preferences": {
          "theme": "light",
          "notifications": false
        }
      },
      "roles": ["user"]
    }
  ],
  "meta": {
    "total": 2,
    "page": 1,
    "hasNext": false
  }
}`;

"keyword">const phpArray = jsonToPhp(complexJson, { 
  beautify: true 
});

console.log(phpArray);

Data Type Handling

"keyword">import { jsonToPhp } "keyword">from '@timkit/json-to-php';

"keyword">const dataTypesJson = `{
  "string": "Hello World",
  "number": 42,
  "float": 3.14159,
  "boolean_true": true,
  "boolean_false": false,
  "null_value": null,
  "empty_string": "",
  "array": [1, 2, 3, "mixed", true],
  "empty_array": [],
  "nested_object": {
    "key": "value"
  }
}`;

"keyword">const phpArray = jsonToPhp(dataTypesJson, { 
  beautify: true,
  indentSize: 2
});

// PHP Output:
// [
//   'string' => 'Hello World',
//   'number' => 42,
//   'float' => 3.14159,
//   'boolean_true' => true,
//   'boolean_false' => false,
//   'null_value' => null,
//   'empty_string' => '',
//   'array' => [1, 2, 3, 'mixed', true],
//   'empty_array' => [],
//   'nested_object' => [
//     'key' => 'value'
//   ]
// ]

Perfect for Every PHP Project

From API integration to data migration - @timkit/json-to-php simplifies your workflow

API Integration

Convert JSON API responses to PHP arrays for seamless integration with PHP applications.

Data Migration

Migrate JSON data from databases or external sources to PHP-compatible formats.

Code Generation

Generate PHP configuration files, fixtures, or test data from JSON templates.

Configuration Files

Transform JSON configuration files into PHP arrays for application settings.

Build Tools

Integrate into build pipelines for generating PHP files from JSON schemas or manifests.

Testing & Fixtures

Create PHP test fixtures and mock data from JSON files for unit testing.

Ready to Convert JSON to PHP?

Join developers using @timkit/json-to-php for seamless JSON to PHP array conversion.

npm install @timkit/json-to-php

Pro Tip: Use beautify option for readable PHP arrays in configuration files.

Questions? Check out the package documentation on NPM.