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.
The proposed solution involves using a form with input fields named in a specific format:
<input name="diameters[0][top]" type="text">
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' ) );
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>'; }
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!