Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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/1/amazon-web-services/13.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 如何在Codeigniter中按页码最多n页取消设置会话数据?_Php_Codeigniter_Session - Fatal编程技术网

Php 如何在Codeigniter中按页码最多n页取消设置会话数据?

Php 如何在Codeigniter中按页码最多n页取消设置会话数据?,php,codeigniter,session,Php,Codeigniter,Session,通过使用此代码,我正在为每个页码取消设置会话中的数据,但问题是在某一点上,我不知道会话中存在多少页数据,例如: $this->session->unset_userdata('current_page_'.$pagenumber); 是否有任何方法可以取消设置会话中的数据,其中的键为“当前页面%” 提前谢谢。试试这个 $this->session->unset_userdata('current_page_'.1); $this->session->unse

通过使用此代码,我正在为每个页码取消设置会话中的数据,但问题是在某一点上,我不知道会话中存在多少页数据,例如:

$this->session->unset_userdata('current_page_'.$pagenumber);
是否有任何方法可以取消设置会话中的数据,其中的键为“当前页面%” 提前谢谢。

试试这个

$this->session->unset_userdata('current_page_'.1);
$this->session->unset_userdata('current_page_'.2);
$this->session->unset_userdata('current_page_'.3);
$this->session->unset_userdata('current_page_'.4);
.
.
.
$this->session->unset_userdata('current_page_'.?????);

此语句将打印所有会话数据,从中可以获得所有页码


打印($this->session->all_userdata())

这应该适合您:

(在这里,我只需获取所有遵循该模式的数组元素。匹配键,而不是获取所有键所使用的值。然后,我只需使用foreach循环在匹配中循环并取消设置数组元素)


您可以尝试搜索会话的首字母(即
当前页面
,并相应地取消设置

Array ( [xy] => 5 [z] => 6 )

演示:

使用
current\u page
作为名称,然后只在会话中存储实际的页码不是更好吗<代码>$this->session->set_userdata('current_page',$page_no)在$page\u否中它将是b数组?我不确定我是否理解您试图做什么,在数组的情况下,您始终可以首先序列化数据:
$this->session->set\u userdata('current\u page',serialize($array))这里有一个问题,因为我不知道存在哪个页码。你应该使用数组,比如,而不是使用当前页面,当前页面等等。你应该使用当前页面[1],当前页面[2]等等。那么需要多少时间?@pakistanimoon你说需要多少时间?比如脚本需要执行多长时间?我只是希望它比其他用户提供的其他解决方案更快?@pakistanimoon我只是将我的答案与此进行了比较:对于10k数组元素,两者的速度几乎相同。在0.02到0.03秒之间执行。只要选择你认为对你帮助最大的答案和剧本,就可以解决你的问题
<?php


    //As an example v Here just use your array
    $array = array("current_page_1" => 1, "current_page_1345" => 2, "current_page_12" => 3, "current_page_34" => 4, "xy" => 5, "z" => 6);
    $sub = preg_grep("/(current_page_)\d+/", array_keys($array));

    foreach($sub as $v)
        unset($array[$v]);  //$this->session->unset_userdata($v);

    print_r($array);

?>
Array ( [xy] => 5 [z] => 6 )
<?php
function startsWith($haystack, $needle) {
    // search backwards starting from haystack length characters from the end
    return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}

foreach($this->session->all_userdata() as $key => $value)
{
    if(startsWith($key, 'current_page_'))
        $this->session->unset_userdata($key);
}
<?php
function startsWith($haystack, $needle) {
    // search backwards starting from haystack length characters from the end
    return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}

$session = array(
            'current_page_12' => 'abc', 
            'current_page_qw1' => 'xyz', 
            'hello' => 'world', 
            'current_page_23d' => 'mno', 
            'example' => '112'
        );

foreach($session as $key => $value)
{
    if(startsWith($key, 'current_page_'))
        unset($session[$key]);
}

print_r($session);
Array
(
    [hello] => world
    [example] => 112
)