2017-06-12 55 views

回答

1

最简单的方法是获得值,去掉项目,并再次设置会话变量。

$data = $_SESSION['array']; // Get the value 
unset($data[1]);    // Remove an item (hardcoded the second here) 
$_SESSION['array'] = $data; // Set the session value with the new array 

更新:
或者像@Qirel说,你可以直接取消设置项目,如果你知道电话号码。

unset($_SESSION['array'][1]); 

更新2
如果你想它的价值要删除的元素,你可以使用array_search找到这个元素的关键。请注意,如果存在具有此值的元素,则只会删除第一个元素。

$value_to_delete = '13/02/2017'; 
if (($key = array_search($value_to_delete, $_SESSION['array'])) !== false) 
    unset($_SESSION['array'][$key]); 
+0

为什么不只是'unset($ _ SESSION ['array'] [1]);'? – Qirel

+0

好点,没想到实际上...... :) – Jerodev

+0

我的意思是,你的工作和实现相同的结果 - 但它的额外两行;-) – Qirel

0

要删除,并从一个数组元素使用未设置()函数:

<?php 
    //session array O 
    unset(O["array"][1]); 
?> 
你要哪个元素去掉,或者你想添加什么