Format PHP Array

Paste a PHP array assignment and get consistent comma spacing, with each statement on its own line. This is a light tidy-up: it does not put one array element per line. If you need that, plan on a manual edit or a dedicated formatter.

How to format a PHP array

  1. 1

    Paste the array code

    Enter the full statement, for example the line that assigns the array to a variable.

  2. 2

    Leave the indent as is

    The indent only affects curly braces, so an array on its own is not changed by it.

  3. 3

    Continue to the result

    Open the formatted code.

  4. 4

    Copy the output

    Check the array still looks right, then copy it.

What happens to an array

The formatter looks at commas and semicolons. Each comma is followed by exactly one space, and each semicolon ends a line. Square brackets, parentheses and the => arrow are left as written.

Input:

<?php $colors=["red","green","blue"];$sizes=[1,2,3];

Result:

<?php $colors=["red", "green", "blue"];
$sizes=[1, 2, 3];

Two arrays, two lines, readable commas.

Nested and associative arrays

Associative arrays work the same way. This input:

<?php $user=["id"=>1,"name"=>"Ana","roles"=>["admin","editor"]];

comes back as:

<?php $user=["id"=>1, "name"=>"Ana", "roles"=>["admin", "editor"]];

The nesting is untouched and everything stays on one line, because there are no semicolons or curly braces inside the array to trigger a break. Spaces around => are not added.

What it will not do

  • It will not start a new line after each element or indent array contents.
  • Square brackets and parentheses never add an indentation level.
  • Commas inside quoted strings also get a space, so a value such as "1,5" becomes "1, 5". Review any string that contains commas before you copy the result.
  • Curly braces, for example in a function that returns the array, are put on separate lines and indented as usual.

A typical use case

Configuration arrays copied from a log, a database field or an API response often arrive with commas jammed against the next value. Running them through this page makes each value easier to tell apart, and gives you a version that is easier to compare by eye with a second array. If you need one key per line, split the result at the commas in your editor. Use find and replace to add a line break after each comma.

Check the result

Count the elements before and after. The number of values must be the same, because the tool only adds spaces after commas. If the count changed, a string contained a comma that you did not expect.

Short arrays and long arrays

Short lists such as [1,2,3] are clear after a single pass. Very long arrays remain one long line, because nothing in them marks a break point. For those, formatting in an editor that understands PHP is the better route. For quick checks and small configuration snippets, this page is enough.

Frequently Asked Questions

No. It only adds a space after commas and breaks lines at semicolons and braces.

No. The arrow keeps whatever spacing you pasted.

Yes. The tool treats both the same, since it only looks at commas, semicolons and braces.

The comma rule runs over the whole text before strings are recognised. Restore the original string value by hand after copying.

Related Tools

Tool available in other languages

Guides and use cases