How to print_r $_POST array?

How to print_r $_POST array?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Debugging $_POST with Multi-Dimensional Input Arrays

When you submit a form containing multiple inputs sharing the same name, such as name="id[]", PHP merges those values into an array within the superglobal $_POST. This behavior is standard for handling collections of data. However, when that data is wrapped inside another array structure—often accidentally or due to specific debugging habits—the default output methods like print_r can be misleading.

In the scenario described, the form sends multiple sets of id[] and value[] fields. The PHP script captures this entire $_POST superglobal and wraps it in a new array: $myarray = array( $_POST);. When iterating through this wrapper using a foreach loop, the code expects to access individual key-value pairs directly. Instead, the iterator receives the integer 0 as the key and the actual $_POST data as the value. This happens because $myarray is a single-element array where the only element is the complex $_POST object itself.

Running print_r($_POST) on the raw superglobal usually yields the expected output: distinct keys like id[] containing arrays of values, and value[] doing the same. However, wrapping it changes the context. The loop prints the key 0 and the string "Array" because the code is echoing the first element of $myarray, which is the entire raw POST data structure, rather than iterating through the individual form fields inside that structure.

To correctly inspect the data, you should remove the unnecessary wrapper array. Accessing $_POST directly allows you to iterate over the actual form keys:

<?php 
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Direct access avoids nesting issues
    print_r($_POST);
}
?>

If your goal is to iterate through the submitted arrays (e.g., accessing each id[] and value[] entry), you must navigate the nested structure explicitly. Since $_POST['id[]'] is itself an array, you would loop through that specific sub-array rather than the top-level $_POST:

<?php 
foreach ($_POST['id[]'] as $index => $id) {
    // Corresponding value exists at the same index in the value[] array
    echo "ID: " . htmlspecialchars($id) . "<br>";
}
?>

This approach assumes that the arrays id[] and value[] maintain a consistent order, which they do when submitted via standard HTML forms. The values align by their position in the sequence. If you attempt to access them by name without checking for existence or array structure first, you may encounter notices about undefined indexes.

Another common pitfall involves how print_r renders multi-dimensional data. When used on a nested array, it outputs a hierarchy of keys and values. If you are debugging complex forms with many fields, the default output can become hard to read quickly. In such cases, using var_dump() provides more context by including data types (e.g., distinguishing between strings and integers) and showing empty slots in arrays clearly. For production logging or error reporting, consider configuring a custom logger that formats these dumps into JSON or structured text files rather than echoing them directly to the browser, which can clutter the UI.

When working with frameworks like Laravel, the superglobals are still available, but the framework encourages using request helpers for cleaner code. For instance, you might use $request->input('id[]') to retrieve the array safely. This method handles cases where the input key doesn't exist without throwing errors, and it filters out empty arrays if configured appropriately.

Ultimately, the issue in the original snippet stems from creating a redundant wrapper around $_POST. By accessing the superglobal directly or targeting the specific sub-keys like id[] and value[], you regain access to the actual form data structure and can debug effectively without confusion.

Our network