如何从 PHP 会话数组中删除变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2231332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove a variable from a PHP session array
提问by LiveEn
I have PHP code that is used to add variables to a session:
我有用于向会话添加变量的 PHP 代码:
<?php
session_start();
if(isset($_GET['name']))
{
$name = isset($_SESSION['name']) ? $_SESSION['name'] : array();
$name[] = $_GET['name'];
$_SESSION['name'] = $name;
}
if (isset($_POST['remove']))
{
unset($_SESSION['name']);
}
?>
<pre> <?php print_r($_SESSION); ?> </pre>
<form name="input" action="index.php?name=<?php echo $list ?>" method="post">
<input type="submit" name ="add"value="Add" />
</form>
<form name="input" action="index.php?name=<?php echo $list2 ?>" method="post">
<input type="submit" name="remove" value="Remove" />
</form>
I want to remove the variable that is shown in $list2from the session array when the user chooses 'Remove'.
$list2当用户选择“删除”时,我想从会话数组中删除显示的变量。
But when I unset, ALL the variables in the array are deleted.
但是当我取消设置时,数组中的所有变量都被删除。
How I can delete just one variable?
如何只删除一个变量?
回答by dnagirl
if (isset($_POST['remove'])) {
$key=array_search($_GET['name'],$_SESSION['name']);
if($key!==false)
unset($_SESSION['name'][$key]);
$_SESSION["name"] = array_values($_SESSION["name"]);
}
Since $_SESSION['name']is an array, you need to find the array key that points at the name value you're interested in. The last line rearranges the index of the array for the next use.
由于$_SESSION['name']是一个数组,您需要找到指向您感兴趣的名称值的数组键。最后一行重新排列数组的索引以供下次使用。
回答by Andreas
To remove a specific variable from the session use:
要从会话中删除特定变量,请使用:
session_unregister('variableName');
(see documentation) or
(见文档)或
unset($_SESSION['variableName']);
NOTE:session_unregister()has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.
注意:session_unregister()自 PHP 5.3.0 起已弃用,自 PHP 5.4.0 起已移除。
回答by Marc B
Is the $_SESSION['name'] variable an array? If you want to delete a specific key from within an array, you have to refer to that exact key in the unset() call, otherwise you delete the entire array, e.g.
$_SESSION['name'] 变量是数组吗?如果要从数组中删除特定键,则必须在 unset() 调用中引用该键,否则删除整个数组,例如
$name = array(0 => 'a', 1 => 'b', 2 => 'c');
unset($name); // deletes the entire array
unset($name[1]); // deletes only the 'b' entry
Another minor problem with your snippet: You're mixing GET query parameters in with a POST form. Is there any reason why you can't do the forms with 'name' being passed in a hidden field? It's best to not mix get and post variables, especially if you use $_REQUEST elsewhere. You can run into all kinds of fun trying to figure out why $_GET['name'] isn't showing up the same as $_POST['name'], because the server's got a differnt EGPCS order set in the 'variables_order' .ini setting.
您的代码段的另一个小问题:您将 GET 查询参数与 POST 表单混合在一起。有什么理由不能做在隐藏字段中传递“名称”的表单?最好不要混合使用 get 和 post 变量,尤其是在其他地方使用 $_REQUEST 时。您可能会遇到各种各样的乐趣,试图弄清楚为什么 $_GET['name'] 与 $_POST['name'] 的显示不同,因为服务器在 'variables_order' 中设置了不同的 EGPCS 顺序.ini 设置。
<form blah blah blah method="post">
<input type="hidden" name="name" value="<?= htmlspecialchars($list1) ?>" />
<input type="submit" name="add" value="Add />
</form>
And note the htmlspecialchars() call. If either $list1 or $list2 contain a double quote ("), it'll break your HTML
并注意 htmlspecialchars() 调用。如果 $list1 或 $list2 包含双引号 ("),它将破坏您的 HTML
回答by Asad Ali
If you want to remove or unset all $_SESSION 's then try this
如果你想删除或取消所有 $_SESSION 的然后试试这个
session_destroy();
If you want to remove specific $_SESSION['name'] then try this
如果你想删除特定的 $_SESSION['name'] 然后试试这个
session_unset('name');
回答by James Campbell
Currently you are clearing the name array, you need to call the array then the index you want to unset within the array:
当前您正在清除名称数组,您需要调用该数组,然后调用要在数组中取消设置的索引:
$ar[0]==2
$ar[1]==7
$ar[2]==9
unset ($ar[2])
Two ways of unsetting values within an array:
在数组中取消设置值的两种方法:
<?php
# remove by key:
function array_remove_key ()
{
$args = func_get_args();
return array_diff_key($args[0],array_flip(array_slice($args,1)));
}
# remove by value:
function array_remove_value ()
{
$args = func_get_args();
return array_diff($args[0],array_slice($args,1));
}
$fruit_inventory = array(
'apples' => 52,
'bananas' => 78,
'peaches' => 'out of season',
'pears' => 'out of season',
'oranges' => 'no longer sold',
'carrots' => 15,
'beets' => 15,
);
echo "<pre>Original Array:\n",
print_r($fruit_inventory,TRUE),
'</pre>';
# For example, beets and carrots are not fruits...
$fruit_inventory = array_remove_key($fruit_inventory,
"beets",
"carrots");
echo "<pre>Array after key removal:\n",
print_r($fruit_inventory,TRUE),
'</pre>';
# Let's also remove 'out of season' and 'no longer sold' fruit...
$fruit_inventory = array_remove_value($fruit_inventory,
"out of season",
"no longer sold");
echo "<pre>Array after value removal:\n",
print_r($fruit_inventory,TRUE),
'</pre>';
?>
So, unset has no effect to internal array counter!!!
所以,unset 对内部数组计数器没有影响!!!
回答by dev-null-dweller
Try this one:
试试这个:
if(FALSE !== ($key = array_search($_GET['name'],$_SESSION['name'])))
{
unset($_SESSION['name'][$key]);
}
回答by Arunraj S
Simply use this method.
只需使用此方法。
<?php
$_SESSION['foo'] = 'bar'; // set session
print $_SESSION['foo']; //print it
unset($_SESSION['foo']); //unset session
?>

