在多维数组存在或不存在时将值插入和替换 [英] Inserting and replacing values to multidimensional array when it exists or not

查看:221
本文介绍了在多维数组存在或不存在时将值插入和替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在用户每次提交表单时替换多维会话数组的初始值。它在数组中不得重复。我无法将每个表单提交的用户输入值插入到会话数组的特定行和列。

I am trying to replace the initial values of a multidimensional session array everytime the user submits a form. It must not have a duplicate in the array. I'm having trouble inserting user input value per form submission to specific row and column of the session array.

所有代码都在一个php文件中。

All codes are in one php file.

<?php 
    session_start();
    $_SESSION['numbers'] = array(
        array(0,0,0,0,0), //row1
        array(0,0,0,0,0), //row2
        array(0,0,0,0,0), //row3
        array(0,0,0,0,0), //row4
        array(0,0,0,0,0) //row5
    );
?>

<?php
    if (isset($_POST["num"]) && !empty($_POST["num"])){
        $userInput = $_POST["num"];
        if(!in_array($userInput, $_SESSION['numbers'])){
            echo "<script>alert('does not exist')</script>";
            //$_SESSION['numbers'] [] = $userInput;
        }else{
            echo "<script>alert('exists')</script>";
            //don't add to array
        }
    }

        echo "<table border = 1>";
        for($row = 0; $row < sizeof($_SESSION['numbers']); $row++){
            echo "<tr>";
            for($col = 0; $col < sizeof($_SESSION['numbers']); $col++){     
                echo "<td>".$_SESSION['numbers'][$row][$col]."</td>";
            }
            echo "</tr>";
        }
        echo "</table>";    

?>

但是,如果我愿意

$_SESSION['numbers'] = $userInput;

它仅用1个值替换整个多维数组,而不是将其插入到特定的行和列

it replaces the entire multidimensional array with just 1 value instead of inserting it to the specific row and column of the nested array.

如果输入为,假设 1 ,并且用户单击了Submit按钮,则我在表的每一行得到的输出并且列只是 1

If input is, lets say 1 and the user hits the submit button, the output I get in table for each row and column is just 1

我无法正确地将其插入到特定行< tr> 和column < td>

I can't properly insert it in a specific row<tr> and column<td>.

如果是一维数组很容易,但是我不知道不知道如何在嵌套的多维数组中执行此操作。请帮忙。

It's easy if it's one dimensional array but I don't know how to do it in multidimensional arrays nested. Please help.

谢谢。

推荐答案

每行在您的数组中都有数字索引;例如,第1行是索引0,第2行是索引1,依此类推。您需要做的就是提交带有特定行号的表单,这样您就可以知道将其插入到哪里。有了这些,以下将为您解决问题。

Each of the rows has numeric index in your array; e.g., row 1 is index 0, row 2 is index 1, etc. What you need to do is have the form submitted with a specific row number, that way you'll know where it needs to be inserted. With that in place, the following will do the trick for you.

然后,您可以迭代行并将其添加(如果尚不存在)。

Then, you can iterate the rows and add it in if it doesn't already exist.

foreach($_SESSION['numbers'] as $_row_index => &$_row) {
    if($_row_index == (int)$_POST['row_index']) {
        if (!in_array((int)$_POST['num'], $_row, true)) {
            $_row[] = (int)$_POST['num']; // Add the num.
        }
    }
}

这篇关于在多维数组存在或不存在时将值插入和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆