简体   繁体   English

从Cookie数组中删除值

[英]Removing a value from a cookie array

I am trying to remove a value from a cookie array:- 我正在尝试从Cookie数组中删除一个值:-

  if (!empty($_GET['job_unfav'])) {

        $value_to_delete = $_GET['job_fav']; // Cookie value to delete from array
        $favouriteIDs = explode('|', $_COOKIE['job_fav']);

        var_dump($favouriteIDs);
        unset($favouriteIDs[$value_to_delete]);

        $favouriteIDs = array_values($favouriteIDs);

        setcookie('job_fav', implode('|', $favouriteIDs));

    }

$favouriteIDs returns:- $favouriteIDs返回:-

array(90) { 
      [0]=> string(3) "542" 
      [1]=> string(0) "" 
      [2]=> string(3) "538" 
      [3]=> string(0) "" 
      [4]=> string(3) "534" 
      [5]=> string(0) "" 
      [6]=> string(3) "524" 
      [7]=> string(0) "" 
      [8]=> string(3) "516" 
      [9]=> string(9) "undefined" 
      [10]=> string(9) "undefined" 
      [11]=> string(9) "undefined" 
      [12]=> string(3) "468" 
      [13]=> string(0) "" 
      [14]=> string(0) "" 
      [15]=> string(0) "" [16]=> string(0) "" [17]=> string(0) "" [18]=> string(0) "" [19]=> string(0) "" [20]=> string(0) "" [21]=> string(0) "" [22]=> string(3) "468" [23]=> string(0) "" [24]=> string(3) "235" [25]=> string(3) "231" [26]=> string(0) "" [27]=> string(0) "" [28]=> string(0) "" [29]=> string(3) "235" [30]=> string(3) "231" [31]=> string(3) "228" [32]=> string(0) "" [33]=> string(0) "" [34]=> string(0) "" [35]=> string(0) "" [36]=> string(3) "235" [37]=> string(3) "231" [38]=> string(0) "" [39]=> string(0) "" [40]=> string(0) "" [41]=> string(3) "231" [42]=> string(3) "228" [43]=> string(3) "225" [44]=> string(0) "" [45]=> string(0) "" [46]=> string(0) "" [47]=> string(0) "" [48]=> string(3) "235" [49]=> string(0) "" [50]=> string(0) "" [51]=> string(0) "" [52]=> string(0) "" [53]=> string(0) "" [54]=> string(0) "" [55]=> string(3) "235" [56]=> string(3) "235" [57]=> string(0) "" [58]=> string(3) "235" [59]=> string(0) "" [60]=> string(0) "" [61]=> string(3) "235" [62]=> string(0) "" [63]=> string(0) "" [64]=> string(0) "" [65]=> string(9) "undefined" [66]=> string(0) "" [67]=> string(9) "undefined" [68]=> string(0) "" [69]=> string(3) "502" [70]=> string(0) "" [71]=> string(0) "" [72]=> string(0) "" [73]=> string(0) "" [74]=> string(0) "" [75]=> string(0) "" [76]=> string(0) "" [77]=> string(0) "" [78]=> string(0) "" [79]=> string(0) "" [80]=> string(0) "" [81]=> string(0) "" [82]=> string(0) "" [83]=> string(0) "" [84]=> string(0) "" [85]=> string(0) "" [86]=> string(0) "" [87]=> string(0) "" [88]=> string(0) "" [89]=> string(3) "514" }

At the moment this isn't removing anything, any idea how to do this? 目前这还没有删除任何内容,您知道如何执行此操作吗?

ie I would like to remove all occurences of '542' from job_fav cookie but keep all other values 即我想从job_fav cookie中删除所有出现的“ 542”,但保留所有其他值

Here is a simple method using the array_search() function. 这是使用array_search()函数的简单方法。

<?php
$favoriteIds = array("542", "","538", "", "534", "", "524" );

$value_to_delete = '542';    //$_GET['job_fav']

$idx = array_search($value_to_delete, $favoriteIds);

if ( $idx !== FALSE ) {
    //if the value was found
    unset($favoriteIds[$idx]);
}

print_r($favoriteIds);

The results would be 结果将是

Array
(
    [1] =>
    [2] => 538
    [3] =>
    [4] => 534
    [5] =>
    [6] => 524
)

In your code your correctly remove the value you want from the array. 在代码中,从数组中正确删除所需的值。

But, In order to override the cookie you already have (so the value you remove is actually removed by exchanging the cookie) you need to add more params to the setcookie php function call you make: 但是,为了覆盖您已经拥有的cookie(因此通过交换cookie实际上删除了您删除的值),您需要向您进行的setcookie php函数调用添加更多参数:

From the docs : 文档

bool setcookie ( string $name [, string $value [, int $ expire = 0 [, string $ path [, string $ domain [, bool $secure = false [, bool $httponly = false ]]]]]] ) bool setcookie(字符串$ name [,字符串$ value [,整数$ expire = 0 [,字符串$ 路径 [,字符串$ domain [,布尔$ secure =假[,布尔$ httponly =假]]]]]]]))

Make sure the params: 确保参数:

path 路径

domain

Are equal than the cookie you already have, otherwise you will just create another one or the setcookie will be ignored. 等于您已经拥有的cookie,否则您将创建另一个cookie,否则setcookie将被忽略。

Then the expire field ( which can be left as 0 if the cookie does not expire ) must be set with the current timestamp, you can get using the time() function plus, the number of seconds you want the cookie to be active: 然后必须使用当前时间戳设置expire字段( 如果cookie没有过期则可以保留为0 ),您可以使用time()函数加上希望cookie处于活动状态的秒数:

$expire = time() + NUMBER_OF_SECONDS $ expire = time()+ NUMBER_OF_SECONDS

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM