简体   繁体   English

我在php中遇到多维数组的未定义索引错误

[英]I'm getting undefined index error for multidimensional array in php

I'm new to php so I was trying to code a program which adds two 3x3 matrices. 我是php新手,所以我尝试编写一个添加两个3x3矩阵的程序。 I'm getting undefined symbol error multiple times in php code. 我在php代码中多次遇到未定义的符号错误。
Is there any different way to define multidimensional arrays in php.` 有没有其他方法可以在php中定义多维数组。
There are already many programs on how resolve this problem for one dimensional array. 关于如何解决一维数组的问题,已经有许多程序。 They are hard coding default values. 它们是默认值的硬编码。 Giving default values to each element in array will be time consuming. 为数组中的每个元素提供默认值将非常耗时。
Here is my code: 这是我的代码:

    <html>
    <head></head>
    <body>
        <form action="matAdd.php" method="post">
            Enter values in first matrix:
            <table cellpadding=10 cellspacing=10>
            <tr>
            <td>
            <table>
                <tr><td colspan="3">Matrix 1</td>
                </tr>
                <tr>
                    <td><input type="number" name="m00" size="3" required/></td>
                    <td><input type="number" name="m01" size="3" required/></td>
                    <td><input type="number" name="m02" size="3" required/></td>
                </tr>
                <tr>
                    <td><input type="number" name="m10" size="3" required/></td>
                    <td><input type="number" name="m11" size="3" required/></td>
                    <td><input type="number" name="m12" size="3" required/></td>
                </tr>
                <tr>
                    <td><input type="number" name="m20" size="3" required/></td>
                    <td><input type="number" name="m21" size="3" required/></td>
                    <td><input type="number" name="m22" size="3" required/></td>
                </tr>
            </table>
            </td>
            <td>
            <table>
                <tr>
                    <td colspan="3">Matrix 2</td>
                </tr>
                <tr>
                    <td><input type="number" name="n00" size="3" required/></td>
                    <td><input type="number" name="n01" size="3" required/></td>
                    <td><input type="number" name="n02" size="3" required/></td>
                </tr>
                <tr>
                    <td><input type="number" name="n10" size="3" required/></td>
                    <td><input type="number" name="n11" size="3" required/></td>
                    <td><input type="number" name="n12" size="3" required/></td>
                </tr>
                <tr>
                    <td><input type="number" name="n20" size="3" required/></td>
                    <td><input type="number" name="n21" size="3" required/></td>
                    <td><input type="number" name="n22" size="3" required/></td>
                </tr>
            </table>
            </td>
            </tr>
            </table>
            <center><input type="submit" value="Add"/></center>
        </form>  
    </body>
</html>
    <?php 
            $m = array(array());
            $n =array(array());
            $sum= array(array());
            for($i=0;$i<3;$i++) 
            {
                for($j=0;$j<3;$j++)
                {
                    $m[$i][$j]=$_POST['m'.$i.$j];
                    $n[$i][$j]=$_POST['n'.$i.$j];
                    $sum[$i][$j]=$m[$i][$j]+$n[$i][$j];
                }
            }
            echo 'Addition is :';
            echo '<table border=1 style="border-collapse:collaspe">';
            for($i=0;$i<3;$i++)
            {
                echo '<tr>';
                for($j=0;$j<3;$j++)
                {
                    echo '<td>'.$sum[$i][$j].'</td>';
                }
                echo '</tr>';
            }
            echo '</table>';
    ?>

I'm getting error in that nested for loop where I am taking values from post method into multidimensional array. 我在嵌套的for循环中遇到错误,在该循环中,我将post方法中的值转换为多维数组。

just try to change following 只是尝试改变以下

1. change your <form> with 1.更改您的<form>

<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post">

2 . 2。 add name attribute to your submit button 将名称属性添加到您的提交按钮

<input type="submit" name ="submit" value="Add"/>

3. put your php code inside 3.把你的PHP代码里面

if(isset($_POST['submit'])){
    $m = array(array());
    $n =array(array());
    //your code....
}

hope this solve your issue 希望这能解决您的问题

A bit of explanation. 有点解释。 Your originall problem was caused mainly by $_POST['m'.$i.$j] . 您的originall问题主要是由$_POST['m'.$i.$j] At the time of the first execution of the php script they were not defined, because no data from form was sent yet, and the named values simply were not yet added to $POST variable. 在第一次执行php脚本时,尚未定义它们,因为尚未发送任何来自表单的数据,并且命名值根本还没有添加到$POST变量中。

The check if(isset($_POST['submit'])) { basically checks if any forms with data wre submitted and executes the code inside the {...} only if there were. check if(isset($_POST['submit'])) {基本上检查是否提交了任何带有数据的表格,并仅在{...}内部执行代码。

So if you make your code look like 因此,如果您使代码看起来像

if(isset($_POST['submit'])){
//Your original php code goes here
}

it will work, because your code will only execute when a form was submitted. 它将起作用,因为您的代码仅在提交表单时执行。

It's not a good built explanation, but it's the best I can do 这不是一个很好的解释,但这是我所能做的最好的解释

I'm getting undefined index error for multidimensional array in php description : Your code is right. 我在php说明中遇到多维数组的未定义索引错误:您的代码正确。 Just initialize array as foolows. 只需将数组初始化为foolows。 If again error occour then check your inform form 如果再次出现错误,请检查您的通知表

<?php 
      $m = array();
      $n =array();

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM