JSON To PHP
is an npm package that converts JSON strings into PHP-style associative arrays. It is useful when working
with JavaScript applications that need to generate PHP code for dynamic content or server-side processing.
You can install the package via npm:
npm install @timkit/json-to-php
interface Options {
beautify?: boolean;
indentLevel?: number;
indentSize?: number;
useTabs?: boolean;
}
The Options interface allows you to customize the behavior of the jsonToPhp function. Below are the available properties:
beautify
(optional, boolean): When set to true, the PHP array is formatted with line breaks and indentation for readability. Default is false.indentLevel
(optional, number): Specifies the initial indentation level for the PHP array. Default is 0.indentSize
(optional, number): Specifies the number of spaces for indentation when beautify is set to true. Ignored whenuseTabs
istrue
Default is 4.useTabs
(optional, boolean): When set to true, tabs are used for indentation instead of spaces. Default is false.
After installation, you can import and use jsonToPhp
to convert JSON strings into PHP array format.
The jsonToPhp
function takes a JSON string and converts it to a PHP-style associative array.
import {jsonToPhp} from '@timkit/json-to-php';
const jsonString = '{"name": "John", "age": 30}';
const phpArray = jsonToPhp(jsonString);
console.log(phpArray);
// ['name' => 'John', 'age' => 30]
The package supports nested JSON objects, converting them into nested PHP arrays.
const nestedJson = '{"user": {"name": "Alice", "email": "[email protected]"}, "active": true}';
console.log(jsonToPhp(nestedJson));
// ['user' => ['name' => 'Alice', 'email' => '[email protected]'], 'active' => true]
You can also convert JSON arrays into PHP arrays.
const jsonArray = '{"fruits": ["apple", "banana", "cherry"]}';
console.log(jsonToPhp(jsonArray));
// ['fruits' => ['apple', 'banana', 'cherry']]
You can beautify the output using the beautify
option, which adds line breaks and indentation to make the PHP array
more readable.
const jsonString = '{"name": "John", "address": {"city": "New York", "zip": "10001"}}';
console.log(jsonToPhp(jsonString, {beautify: true}));
// [
// 'name' => 'John',
// 'address' => [
// 'city' => 'New York',
// 'zip' => '10001'
// ]
// ]
The indentLevel
option allows you to specify the inital indentation level for the PHP array.
console.log(jsonToPhp(jsonString, {beautify: true, indentLevel: 2}));
// [
// 'name' => 'John',
// 'address' => [
// 'city' => 'New York',
// 'zip' => '10001'
// ]
// ]
You can adjust the indentation size:
console.log(jsonToPhp(jsonString, {beautify: true, indentSize: 2}));
// [
// 'name' => 'John',
// 'address' => [
// 'city' => 'New York',
// 'zip' => '10001'
// ]
// ]
You can use tabs for indentation by setting the useTabs
option to true
.
console.log(jsonToPhp(jsonString, {beautify: true, useTabs: true}));
// [
// 'name' => 'John',
// 'address' => [
// 'city' => 'New York',
// 'zip' => '10001'
// ]
// ]
The package supports different JSON data types including strings, numbers, booleans, and null values.
const jsonWithTypes = '{"isAdmin": true, "balance": 100.5, "preferences": null}';
console.log(jsonToPhp(jsonWithTypes));
// ['isAdmin' => true, 'balance' => 100.5, 'preferences' => null]
If you provide an empty JSON string, the package will return an empty PHP array.
console.log(jsonToPhp('{}'));
// []
If the provided string is not valid JSON, it will throw an error.
try {
jsonToPhp('{invalid}');
} catch (error) {
console.error('Invalid JSON format');
}
The project is organized into the following directories:
src/
- Source TypeScript filesdist/
- Compiled JavaScript output (generated during build)test/
- Test files
Apache-2.0