PHP Array to Object
Move an array out of PHP and into a JavaScript object you can paste straight in.
Your result will appear hereThis reads literal values only. If the array contains a variable, a PHP constant or a function call, it says so rather than guessing at the value.
Converted entirely in your browser — nothing is sent to a server.
About this tool
PHP array to object turns an array written in PHP source into a JavaScript object literal you can paste straight into your code.
The difference from the JSON version is that keys which are valid identifiers are left unquoted — the way real JavaScript is written, and easier to read. Keys with hyphens, spaces or a leading digit stay quoted, because unquoted they would not parse.
The usual job is moving constants or lookup tables across the stack, from a PHP backend to a JavaScript frontend: a province list, a product-code mapping, a set of form defaults.
It shares its parser with PHP array to JSON and differs only in how the result is printed, so whatever one page can read, the other can too — and both say so plainly when they hit a variable or constant they cannot evaluate.
How to use
- 1Copy the array out of your PHP code
- 2Paste it in — both array() and the short bracket syntax work
- 3It becomes an indented JavaScript object
- 4Copy it straight into your JavaScript
Frequently asked questions
- How is this different from converting to JSON?
- The output is a JavaScript object literal: keys that are valid identifiers are left unquoted, which reads better and matches how real JavaScript is written. JSON always quotes every key.
- Which keys still get quoted?
- Any key that is not a valid JavaScript identifier — containing a hyphen or a space, or starting with a digit. Leaving those unquoted would not parse.
- Why did my array with gaps in its keys become an object?
- A PHP array is an ordered map. If the keys are not exactly 0, 1, 2… turning it into a JavaScript array would discard them.
- Are comments supported?
- Yes — //, # and /* */ are all skipped, so you can paste code straight from a real file.
- Is this the same parser as the JSON version?
- Yes. Both pages share one parser and differ only in how the result is written out, so whatever one can read, the other can too.