2017-06-12 56 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]); 
?> 
你要哪個元素去掉,或者你想添加什麼