Coding Tutorial
Convert a List to JavaScript, Python, and PHP Arrays
Learn how to convert a plain text list into JavaScript arrays, Python lists, and PHP arrays with proper quoting, escaping, and copy-ready examples.
When you have a plain list of items—perhaps copied from a spreadsheet, exported from a database, or typed out in a text file—you often need to convert it into an array literal for use in your code. Manually adding brackets, commas, and quotes is tedious and error-prone, especially if the list is long or contains special characters.
Different programming languages have similar but distinct syntaxes for arrays. JavaScript uses square brackets for arrays, Python uses square brackets for lists, and PHP supports both array() and square brackets. Strings require quotes in all three languages, while numbers are written without quotes. Understanding these nuances ensures your converted array is syntactically correct.
This guide compares the conversion process for JavaScript, Python, and PHP. It covers quoting rules, escaping, and handling mixed data types. We also introduce free browser-based tools from CompareTwoLists that automate these conversions with a single paste, keeping your data private.
The Starting Point: A Plain Text List
Before converting, identify your raw input. The simplest case is a list of strings, each on a separate line. This input can come from various sources: a column in a CSV file, a list copied from a spreadsheet, or from a SELECT output in SQL. For our examples, we'll use a list of fruit names.
- One item per line
- No extra formatting or punctuation
- Empty lines are typically ignored
apple
banana
cherry
dateJavaScript Array Syntax and Conversion
In JavaScript, arrays are enclosed in square brackets. Elements are separated by commas. Strings must be wrapped in either single or double quotes. The conversion of our fruit list produces a clean array literal.
You can assign the array to a variable using const, let, or var. Choose const for values that won't be reassigned. Avoid trailing commas for maximum compatibility, though modern JavaScript allows them.
- Wrap each item in quotes
- Separate by commas
- Enclose everything in square brackets
const fruits = ['apple', 'banana', 'cherry', 'date'];Python List Syntax and Conversion
Python lists also use square brackets and commas. Python's convention is to use single quotes for strings, although double quotes are acceptable. The resulting list is a first-class collection type with numerous built-in methods.
As in JavaScript, you assign the list to a variable. Python's list comprehension and built-in functions make working with lists very flexible. The conversion is straightforward, and trailing commas are allowed in Python.
- Use square brackets and commas
- Single quotes are customary for strings
- Trailing comma is valid
fruits = ['apple', 'banana', 'cherry', 'date']PHP Array Syntax and Conversion
PHP provides two ways to declare arrays: the older array() function and the short syntax [] introduced in PHP 5.4. The short syntax is now widely used and recommended for new code. PHP requires a $ prefix on variable names.
Both single and double quotes are supported in PHP. Double quotes allow variable interpolation and escape sequences, but for simple string arrays, single quotes are slightly more efficient and avoid unintended parsing.
- Use $variableName = [...] or $variableName = array(...)
- Do not use trailing commas (not allowed in PHP)
- Prefer single quotes for simple strings
$fruits = ['apple', 'banana', 'cherry', 'date'];Handling Strings Versus Numbers
A common concern is whether to treat an item as a string or a number. In the target array, numbers should not be quoted—they must be numeric literals. If the source list contains numeric-looking values like ZIP codes, product IDs, or phone numbers, you must decide based on context. If you need to preserve leading zeros, treat them as strings.
When the data type is ambiguous, it is safer to quote everything as strings, then adjust manually for numeric values. For example, a list with a mix of names and ages requires careful separation.
- Numbers: no quotes
- Strings: always quotes
- Ambiguous values: default to string
const mixed = ['Alice', 30, 'Bob', 28];mixed = ['Alice', 30, 'Bob', 28]$mixed = ['Alice', 30, 'Bob', 28];Escaping Special Characters and Quotes
When an item contains a quote character that matches the style you are using, it must be escaped. For example, the name O'Brien contains a single quote. If you wrap strings in single quotes, you must escape it as O\'Brien. Alternatively, use double quotes to avoid escaping: "O'Brien" works without modification.
Similarly, if a string contains double quotes, you can wrap it in single quotes. Backslashes and newline characters also require escaping. Choose the quote style that results in the fewest escapes for readability.
- Identify all quote characters in the item
- If the item contains single quotes, either escape them (\') or use double quotes
- If the item contains double quotes, either escape them (\") or use single quotes
- Escape any included backslashes by doubling them (\\)
- Remove any newlines inside items or convert them to \n
- Escaping differs between single and double quote contexts
- Alternate quote styles to minimize escapes
- Use tools to automate proper escaping
const names = ["O'Brien", 'Say "hello"', 'back\\slash'];names = ["O'Brien", 'Say "hello"', 'back\\slash']$names = ["O'Brien", 'Say "hello"', 'back\\slash'];Using CompareTwoLists for Instant Conversion
CompareTwoLists provides separate converters for JavaScript arrays, Python lists, and PHP arrays. Each one treats every input line as a string, applies the selected single- or double-quote escaping rules, and can output a compact or multi-line result.
The JavaScript tool can emit a const declaration, let declaration, or array literal. The Python and PHP tools accept an optional variable name. They do not infer numeric or boolean types, and they do not add trailing commas, so review and edit types explicitly when the destination code requires them.
- Open the converter for the target language.
- Paste one intended string value per line.
- Choose single or double quotes, layout, and the variable or declaration option.
- Copy the result and run it through the target language's formatter or test suite.
- Local in-page processing
- Language-specific string escaping
- Every input line is emitted as a string
- No automatic type inference or trailing comma option
Conclusion
Array syntax is similar across JavaScript, Python, and PHP, but quoting, escaping, variable declarations, and data types still need deliberate choices. Treating pasted lines as strings is a safe default for IDs and human-entered values.
The browser converters create copy-ready string collections locally. Review inferred meaning yourself, then format or test the result in the destination language before committing it to an application.