Home > Backend Development > PHP Tutorial > How to Submit Multidimensional Arrays via POST in PHP?

How to Submit Multidimensional Arrays via POST in PHP?

Linda Hamilton
Release: 2024-11-28 18:43:11
Original
994 people have browsed it

How to Submit Multidimensional Arrays via POST in PHP?

Submitting Multidimensional Arrays via POST in PHP

Context

A multidimensional array is a data structure that organizes data into multiple dimensions. In the case of a form, each row and column represents a dimension. The challenge presented in this discussion is to create a multidimensional array from an unknown number of rows and known number of columns in a PHP form.

Solution

The proposed solution involves using a form with input fields named in a specific format:

<input name="diameters[0][top]" type="text">
Copy after login

This format creates a nested array structure where each row is an element of the outer array, and the top and bottom diameter values are stored as elements of the inner array.

Upon form submission, the $_POST array will contain the multidimensional array in the following format:

$_POST['diameters'] = array(
    array(
        'top' => 'first value',
        'bottom' => 'first value'
    ),
    array(
        'top' => 'second value',
        'bottom' => 'second value'
    )
);
Copy after login

Example Usage

To display the array as a table, you can use the following code:

if ( isset( $_POST['diameters'] ) )
{
    echo '<table>';
    foreach ( $_POST['diameters'] as $diam )
    {
        echo '<tr>';
        echo '  <td>', $diam['top'], '</td>';
        echo '  <td>', $diam['bottom'], '</td>';
        echo '</tr>';
    }
    echo '</table>';
}
Copy after login

This code will generate a simple table with two columns: "Top" and "Bottom" that displays the data from the submitted form.

The above is the detailed content of How to Submit Multidimensional Arrays via POST in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template