
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
@timkit/json-to-php
Advanced tools
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
when useTabs is true 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": "alice@example.com"}, "active": true}';
console.log(jsonToPhp(nestedJson));
// ['user' => ['name' => 'Alice', 'email' => 'alice@example.com'], '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 filesApache-2.0
FAQs
Convert JSON strings into PHP-style associative arrays.
We found that @timkit/json-to-php demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.