Text to PHP

PHP Array Converter

Generate a PHP array from pasted values.

Build the PHP array

PHP Array Converter

0 items / 0 unique
Input Text
0 characters Ctrl+Enter to convert - Drag and drop .txt
Split Input By
Cleaning Options
Output
Output Format
PHP options
Customization
Output 0 unique Output
// Your converted output will appear here.
Equivalent code
Paste text and click Convert Text, or use Ctrl+Enter.
Your text stays private. All conversion happens locally in your browser using JavaScript. Nothing is uploaded to any server.

Pick short [] or classic array() syntax, and single or double quotes — the escaping rules differ between the two, and this tool applies the right set for each.

How It Works

How to use the PHP Array Converter

  1. Paste your values

    One per line by default. Change the delimiter for comma or tab separated input.

  2. Choose the array syntax

    Short [] for anything on PHP 5.4 or newer. array() when the codebase or its style guide still expects it.

  3. Choose the quote style

    Single quotes are literal. Double quotes interpolate variables and interpret escape sequences — pick them only when you need that.

Examples

PHP Array Converter examples

Short array syntax

Input

admin
editor
subscriber

Output

$roles = [
  'admin',
  'editor',
  'subscriber'
];

Classic array() syntax

Input

admin
editor

Output

$roles = array(
  'admin',
  'editor'
);

Still required on PHP 5.3 and older, and still common throughout WordPress and Drupal.

A value containing a dollar sign

Input

$total
plain

Output

$labels = [
  "\$total",
  "plain"
];

With double quotes selected, the dollar sign is escaped so PHP does not try to interpolate a variable.

Notes

Working with php array converter

Single and double quotes escape differently

Inside single quotes PHP recognises exactly two escapes: an escaped apostrophe and an escaped backslash. Everything else is literal, newline sequences included — so "escaping" a newline there would silently insert a backslash and the letter n. Inside double quotes PHP interpolates variables and interprets escape sequences, so the dollar sign and brace must be escaped. This tool applies each rule set correctly rather than using one escaper for both.

Short syntax versus array()

[] has been available since PHP 5.4 and is the modern default. array() is not deprecated and still appears across WordPress, Drupal and older Laravel code. If you are pasting into an existing file, match what is already there — mixed syntax in one file is the kind of thing that gets flagged in review.

Reading the array back

For a list of values, in_array($needle, $roles, true) is the membership test — pass true so it compares types too, otherwise PHP's loose comparison can match 0 against a non-numeric string. If you test membership often, array_flip() plus isset() is the faster shape.

FAQ

PHP Array Converter FAQs

PHP Array Converter

Generate a PHP array from pasted values. Pick short [] or classic array() syntax, and single or double quotes — the escaping rules differ between the two, and this tool applies the right set for each.