Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从PHP会话数组中删除单个元素?_Php_Arrays_Session - Fatal编程技术网

如何从PHP会话数组中删除单个元素?

如何从PHP会话数组中删除单个元素?,php,arrays,session,Php,Arrays,Session,我有一个php会话数组,比如 ('10/01/2017, '13/02/2017', '21/21/2107') 现在,如何在O(1)中添加和删除元素或从该数组中删除元素最简单的方法是获取值,删除项,然后再次设置会话变量 $data = $_SESSION['array']; // Get the value unset($data[1]); // Remove an item (hardcoded the second here) $_SESSION['ar

我有一个
php
会话数组,比如

('10/01/2017, '13/02/2017', '21/21/2107')

现在,如何在O(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
如果要按元素的值删除该元素,可以使用查找该元素的键。请注意,如果存在具有此值的to元素,则仅删除第一个元素

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

最简单的方法是获取该值,删除该项,然后再次设置会话变量

$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
如果要按元素的值删除该元素,可以使用查找该元素的键。请注意,如果存在具有此值的to元素,则仅删除第一个元素

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

要从数组中删除和元素,请使用unset()函数:

 <?php
  //session array O
  unset(O["array"][1]);
 ?>

要从数组中删除和删除元素,请使用unset()函数:

 <?php
  //session array O
  unset(O["array"][1]);
 ?>


要删除哪个元素,或者要添加什么?您需要
unset()
array\u shift()
array\u pop()
,具体取决于您要执行的操作。要删除哪个元素,或者添加什么?您需要
unset()
array\u shift()
array\u pop()
,这取决于您想做什么。为什么不干脆取消设置($\u会话['array'][1])?我的意思是,您的代码将起作用并实现相同的结果-但它需要额外的两行;-)我不知道索引值。。我只是想删除“2017年2月13日”的元素。。我怎么做…?谢谢你,苏much@Jerodev你能帮我解释一下为什么不取消设置($_SESSION['array'][1])?我的意思是,您的代码将起作用并实现相同的结果-但它需要额外的两行;-)我不知道索引值。。我只是想删除“2017年2月13日”的元素。。我怎么做…?谢谢你,苏much@Jerodev你能帮我做点什么吗