如何将用户输入作为矩阵格式在php中执行矩阵添加? 有人有什么建议吗?

How to get user input as a matrix format to perform matrix addition in php? anybody having suggestions?

Give a option to user with two input box . First for rows and second for columns; Generate a table based on these input through javascript having textbox in each and input box name should be in array based on rows and columns while you are generating. let says, user enters :- rows : 3 columns: 3

generate table throrugh javascript your table should be

<table>
<tr>
    <td><input type="textbox" name="matrix[0][]" value=""/> </td>
    <td><input type="textbox" name="matrix[0][]" value=""/> </td>
    <td><input type="textbox" name="matrix[0][]" value=""/> </td>
</tr>
<tr>
    <td><input type="textbox" name="matrix[1][]" value=""/> </td>
    <td><input type="textbox" name="matrix[1][]" value=""/> </td>
    <td><input type="textbox" name="matrix[1][]" value=""/> </td>
</tr>
<tr>
    <td><input type="textbox" name="matrix[2][]" value=""/> </td>
    <td><input type="textbox" name="matrix[2][]" value=""/> </td>
    <td><input type="textbox" name="matrix[2][]" value=""/> </td>
</tr>
</table>

and get a two dimension arrray when form is posted .

<?php
$matrixArr = $_POST['matrix']; // it will be a two dimenssion array having value as  matrix have
?>

The simplest (and possibly most intuitive) way would be to present the user with a <textarea> and ask them to input the matrix values in the following format:

a b
c d

Values in individual rows are delimited by spaces, and rows are delimited by newlines. Here's a quick and easy way to generate a matrix out of the submitted <textarea>:

<?php

  $txt = $_POST['matrix'];

  $mat = explode ("
", $txt);

  for ($i = 0; $i < sizeof ($mat); ++$i)
    $mat[$i] = explode (' ', $mat[$i]);

?>

Of course, the same code would work for values delimited by comma, or any other delimiter you see fit.

A less intuitive way would be to present the user with a literal matrix of <input> fields.

You can do, using input fields.

<?php
function show_matrix($num, $cols, $rows){
for($i=0; $i<$rows; $i++){
    for($j=0; $j<$cols; $j++){
        echo '<input size=2 name="matr['.$num.']['.$i.']['.$j.']"/>';
    }
    echo '<br/>';
}
}
show_matrix(0, 3, 3);
echo '<br/>';
show_matrix(1, 3, 3);
?>

first matrix: $_POST['matr'][0], second: $_POST['matr'][1]