如何在 Codeigniter 中按页码取消设置会话数据最多 n 页?
How to unset session data by page number upto n pages in Codeigniter?
$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_'.?????);
有什么方法可以取消设置像 "current_page_%" 这样的密钥的会话中的数据
提前致谢。
试试这个
$j = 1;
for($i=0;$i==$j;$i++)
{
if($this->session->userdata('current_page_'.$i))
{
$this->session->unset_userdata('current_page_'.$i);
$j++;
}else
{
break;
}
}
此语句将打印所有会话数据,您可以从中获取所有页码。
print_r($this->session->all_userdata());
这应该适合你:
(这里我只是使用 preg_grep()
to get all array elements which follows the pattern. To match the keys and not the values I use array_keys()
来获取所有键。然后我只是用 foreach 循环遍历匹配项并取消设置数组元素)
<?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 )
您可以尝试搜索会话的首字母 (即 current_page_
) 并相应地取消设置。
<?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
)
$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_'.?????);
有什么方法可以取消设置像 "current_page_%" 这样的密钥的会话中的数据 提前致谢。
试试这个
$j = 1;
for($i=0;$i==$j;$i++)
{
if($this->session->userdata('current_page_'.$i))
{
$this->session->unset_userdata('current_page_'.$i);
$j++;
}else
{
break;
}
}
此语句将打印所有会话数据,您可以从中获取所有页码。
print_r($this->session->all_userdata());
这应该适合你:
(这里我只是使用 preg_grep()
to get all array elements which follows the pattern. To match the keys and not the values I use array_keys()
来获取所有键。然后我只是用 foreach 循环遍历匹配项并取消设置数组元素)
<?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 )
您可以尝试搜索会话的首字母 (即 current_page_
) 并相应地取消设置。
<?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
)