如何从显示在 table 中的 session 数组中删除一个值?

How can I remove a value from my session array which is displayed within a table?

我在 session 中创建了一个数组,我已成功地将其放入 table。它实际上只是用户可以通过单击产品页面上的按钮创建的收藏夹列表。

但是,我想在 table 中的每个产品旁边创建一个按钮,用于从数组中删除该产品,但我无法弄清楚为什么它不起作用。

这是我的代码:(已更新)

    <?php
            if (isset($_POST['remove'])) {
            $value_to_delete = 'JX-1E1-LTU';
            if(($key = array_search($value_to_delete, $_SESSION['arr'])) !== false) {
                unset($_SESSION['arr'][$key]);
                $_SESSION["arr"] = array_values($_SESSION["arr"]);
                    }
            }           
        ?>

         <?php

            $contents = ($_SESSION['arr']);
            $arrlength = count($contents);

                echo '<table class="table table-striped equipment">';
                echo '<thead>';
                echo '<tr>';
                echo '<th scope="col">';
                echo 'Parts';
                echo '</th>';
                echo '<th scope="col">';
                echo 'Remove Item';
                echo '</th>';
                echo '</tr>';
                echo '</thead>';
                echo '<tbody>';                 
                for($x = 0; $x < $arrlength; $x++) 
                {
                echo '<tr>';
                echo '<td>';
                echo $contents[$x];                     
                    $part = $contents[$x];                      
                echo '</td>';
                echo '<td>';                        
                    $pos = array_search($part, $contents);    
                    echo ' ' . $pos;                                    
                ?>  
                <form action="" method="post">
                <input type="text" name="value" value="<?php echo $part;?>">
                <input type="submit" name="remove" value="Remove">
                </form>         
                <?php                       
                echo '</td>';
                echo '</tr>';                   
                }                   
                echo'</tbody>';
                echo '</table>';
?>

'deleteall' 按钮位于页面底部 - 它成功地从我的 session 中删除了所有值,但 'remove' 按钮并没有单独删除每个产品:

            if (isset($_POST['remove'])) {
                $key=array_search($_GET[$part],$_SESSION['arr']);
                if($key!==false)
                unset($_SESSION['arr'][$key]);
                $_SESSION["arr"] = array_values($_SESSION["arr"]);
            }

            echo '<form action="" method="post">';
            echo    '<input type="submit" name="remove" value="Remove">';
            echo '</form>';     

我猜我在上面的部分做错了什么,但我似乎碰壁了并尝试了我能想到的一切,任何帮助将不胜感激!

您的第二个示例似乎暗示您在表单显示之前执行 remove 操作,因此在显示 table 之后。它不会在第一页刷新时起作用。

因此,除非删除操作代码不在正确的位置,否则请尝试下面的这段代码,它在这里有效。

首先将其作为单个文件使用。按照说明进行操作,看看是否可以重现您的问题。如果不是,可能与环境有关。

然后,在显示 table 之前放置块代码(并使用正确的值编辑 $_SESSION["arr"] :)),看看它是否从数组中删除了正确的元素。

如果没有,请尝试通过日志记录进行调试。

<?php
session_start();

echo "<pre>";

// Load this page once. Then, before the next page refresh,
// Comment the below line to check if the session values
// are updated

$_SESSION["arr"] = array("trousers", "blue", "jean");
print_r($_SESSION);

unset($_SESSION["arr"][array_search("blue", $_SESSION["arr"])]);
print_r($_SESSION);

$_SESSION["arr"] = array_values($_SESSION["arr"]);
print_r($_SESSION);

echo "</pre>";

?>