我正在尝试从cookie数组中删除一个值: -
$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" }
返回: -
job_fav
目前还没有删除任何内容,知道怎么做?
即。我想删除所有出现的“542'来自$("input").keypress(function(e) {
if ( event.which == 13 ) {
event.preventDefault();
}
});
Cookie但保留所有其他值
答案 0 :(得分:1)
这是使用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);
结果将是
Array
(
[1] =>
[2] => 538
[3] =>
[4] => 534
[5] =>
[6] => 524
)
答案 1 :(得分:0)
在您的代码中,您可以从阵列中正确删除所需的值。
但是, 为了覆盖你已经拥有的cookie(所以删除的值实际上是通过交换cookie删除的)你需要在你做的setcookie php函数调用中添加更多参数:
来自docs:
bool setcookie(string $ name [,string $ value [,int $ expire = 0 [,string $ 路径 [,字符串$ 域 [,bool $ secure = false [,bool $ httponly = false]]]]]])
确保参数:
<强>路径强>
<强>域强>
等于你已经拥有的cookie,否则你只会创建另一个或者setcookie将被忽略。
然后 expire 字段(如果cookie未过期可以保留为0 )必须使用当前时间戳设置,您可以使用{{ 3}} function plus,你希望cookie活动的秒数:
$ expire = time()+ NUMBER_OF_SECONDS
我希望这会有所帮助。