如何从PHP会话数组中删除变量

时间:2021-09-19 13:27:24

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 $list2 from 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?

如何只删除一个变量?

6 个解决方案

#1


49  

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']是一个数组,您需要找到指向感兴趣的名称值的数组键。最后一行重新排列数组的索引以便下次使用。

#2


40  

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中被删除。

#3


4  

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表单混合在一起。有什么理由不能在隐藏字段中传递“name”的表单呢?最好不要混合get和post变量,尤其是在其他地方使用$_REQUEST时。您可能会遇到各种各样的乐趣,试图弄清楚为什么$_GET['name']不显示与$_POST['name']相同,因为服务器在'variables_order' .ini设置中设置了不同的EGPCS订单。

<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

#4


1  

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对内部数组计数器没有影响!!!

http://us.php.net/unset

http://us.php.net/unset

#5


1  

If you want to remove or unset all $_SESSION 's then try this

如果您想删除或取消设置所有$_SESSION 's,那么尝试一下。

session_destroy();

If you want to remove specific $_SESSION['name'] then try this

如果您想删除特定的$_SESSION['name'],那么试试这个

session_unset('name');

#6


0  

Try this one:

试试这个:

if(FALSE !== ($key = array_search($_GET['name'],$_SESSION['name'])))
{
    unset($_SESSION['name'][$key]);
}

#1


49  

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']是一个数组,您需要找到指向感兴趣的名称值的数组键。最后一行重新排列数组的索引以便下次使用。

#2


40  

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中被删除。

#3


4  

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表单混合在一起。有什么理由不能在隐藏字段中传递“name”的表单呢?最好不要混合get和post变量,尤其是在其他地方使用$_REQUEST时。您可能会遇到各种各样的乐趣,试图弄清楚为什么$_GET['name']不显示与$_POST['name']相同,因为服务器在'variables_order' .ini设置中设置了不同的EGPCS订单。

<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

#4


1  

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对内部数组计数器没有影响!!!

http://us.php.net/unset

http://us.php.net/unset

#5


1  

If you want to remove or unset all $_SESSION 's then try this

如果您想删除或取消设置所有$_SESSION 's,那么尝试一下。

session_destroy();

If you want to remove specific $_SESSION['name'] then try this

如果您想删除特定的$_SESSION['name'],那么试试这个

session_unset('name');

#6


0  

Try this one:

试试这个:

if(FALSE !== ($key = array_search($_GET['name'],$_SESSION['name'])))
{
    unset($_SESSION['name'][$key]);
}