使用前缀设置动态会话变量(数组)并使用 foreach 循环
Setting dynamic session variables(arrays) with a prefix and looping with foreach
我正试图在 php 中完成以下任务:
- 在会话变量中设置动态数组。
- 为这些会话变量添加名为 order_ 的前缀。
- 遍历以前缀 order_ 开头的会话变量。
这是我目前的代码:
foreach($array as $subarray) {
foreach($subarray as $subset) {
$nomInput = $subset['Nom'];
$inputArray=[
1=>[
['Nom'=>$input->get($nomInput, null, 'string'),
'LabelFr'=>$subset['LabelFr'],
'LabelEn'=>$subset['LabelEn']]
]
];
$session->set('order_'.$nomInput, $inputArray);
}
}
使用这段代码,我可以使用前缀正确设置变量。
但是,我找不到使用 foreach 循环遍历结果的方法。
有人能给我一些指示,说明如何使用 foreach 循环仅操作具有前缀 order_ 的会话变量吗?
非常感谢!
根据 Joomla JSession documentation,JSession
class 确实提供了返回 ArrayIterator
的 getIterator
方法。
作为一种可重复使用的方法,您可以实现自己的 FilterIterator
class,仅迭代具有特定前缀的项目并 可选地 从键。
在您的代码中,您通过
获得迭代器
$sessionArrayIter = $session->getIterator();
由于我对Joomla不是很了解,也没有安装过运行,所以就恶搞一下:
$sessionArray = ['aa_test1' => 1, 'bb_test2' => 2, 'aa_test3' => 3, 'cc_test4' => 4];
$sessionArrayIter = new ArrayIterator($sessionArray);
Class 实施
然后我们实现 PrefixFilterIterator
class 扩展 PHP 的摘要 FilterIterator
class.
class PrefixFilterIterator extends FilterIterator
{
private
$_prefix,
$_prefixLength,
$_strip_prefix
;
public function __construct(Iterator $iterator, string $prefix, bool $strip_prefix = false)
{
parent::__construct($iterator);
$this->set_prefix($prefix, $strip_prefix);
}
public function set_prefix(string $prefix, ?bool $strip_prefix = null) : void
{
$this->_prefix = $prefix;
$this->_prefixLength = strlen($prefix);
if(null !== $strip_prefix)
$this->_strip_prefix = $strip_prefix;
}
// conditionally remove prefix from key
public function key() /* : mixed scalar */
{
return $this->_strip_prefix ? substr(parent::key(), $this->_prefixLength) : parent::key();
}
// accept prefixed items only
public function accept() : bool
{
return 0 === strpos(parent::key(), $this->_prefix);
}
}
用法
为了迭代筛选的项目,我们创建了一个新的迭代器实例。
$prefixIter = new PrefixFilterIterator($sessionArrayIter, 'aa_', true);
foreach ($prefixIter as $k => $v)
echo "$k => $v", PHP_EOL;
输出
test1 => 1
test3 => 3
备注、限制、待办事项:
上面的代码在 PHP >= 7.1
上运行
要支持 PHP 7.0,必须调整类型提示。 :void
在 PHP<7.1 中不受支持,必须删除,同样 ?bool
必须更改为 bool
。
这是一个简单的实现,将问题集中在问题中以减少答案中的 'noise'。 mbstring
是 PHP 的非默认扩展。因此我没有使用多字节字符串函数。但是,数组键可能包含多字节字符集。为了支持这样的键,将需要一些字符串函数包装器的有条件实现,如果安装了适当的函数,则使用它们。带有 u
修饰符的 preg_*
函数可以作为支持多字节 unicode 键的替代方法。
我正试图在 php 中完成以下任务:
- 在会话变量中设置动态数组。
- 为这些会话变量添加名为 order_ 的前缀。
- 遍历以前缀 order_ 开头的会话变量。
这是我目前的代码:
foreach($array as $subarray) {
foreach($subarray as $subset) {
$nomInput = $subset['Nom'];
$inputArray=[
1=>[
['Nom'=>$input->get($nomInput, null, 'string'),
'LabelFr'=>$subset['LabelFr'],
'LabelEn'=>$subset['LabelEn']]
]
];
$session->set('order_'.$nomInput, $inputArray);
}
}
使用这段代码,我可以使用前缀正确设置变量。 但是,我找不到使用 foreach 循环遍历结果的方法。
有人能给我一些指示,说明如何使用 foreach 循环仅操作具有前缀 order_ 的会话变量吗?
非常感谢!
根据 Joomla JSession documentation,JSession
class 确实提供了返回 ArrayIterator
的 getIterator
方法。
作为一种可重复使用的方法,您可以实现自己的 FilterIterator
class,仅迭代具有特定前缀的项目并 可选地 从键。
在您的代码中,您通过
获得迭代器$sessionArrayIter = $session->getIterator();
由于我对Joomla不是很了解,也没有安装过运行,所以就恶搞一下:
$sessionArray = ['aa_test1' => 1, 'bb_test2' => 2, 'aa_test3' => 3, 'cc_test4' => 4];
$sessionArrayIter = new ArrayIterator($sessionArray);
Class 实施
然后我们实现 PrefixFilterIterator
class 扩展 PHP 的摘要 FilterIterator
class.
class PrefixFilterIterator extends FilterIterator
{
private
$_prefix,
$_prefixLength,
$_strip_prefix
;
public function __construct(Iterator $iterator, string $prefix, bool $strip_prefix = false)
{
parent::__construct($iterator);
$this->set_prefix($prefix, $strip_prefix);
}
public function set_prefix(string $prefix, ?bool $strip_prefix = null) : void
{
$this->_prefix = $prefix;
$this->_prefixLength = strlen($prefix);
if(null !== $strip_prefix)
$this->_strip_prefix = $strip_prefix;
}
// conditionally remove prefix from key
public function key() /* : mixed scalar */
{
return $this->_strip_prefix ? substr(parent::key(), $this->_prefixLength) : parent::key();
}
// accept prefixed items only
public function accept() : bool
{
return 0 === strpos(parent::key(), $this->_prefix);
}
}
用法
为了迭代筛选的项目,我们创建了一个新的迭代器实例。
$prefixIter = new PrefixFilterIterator($sessionArrayIter, 'aa_', true);
foreach ($prefixIter as $k => $v)
echo "$k => $v", PHP_EOL;
输出
test1 => 1
test3 => 3
备注、限制、待办事项:
上面的代码在 PHP >= 7.1
上运行要支持 PHP 7.0,必须调整类型提示。 :void
在 PHP<7.1 中不受支持,必须删除,同样 ?bool
必须更改为 bool
。
这是一个简单的实现,将问题集中在问题中以减少答案中的 'noise'。 mbstring
是 PHP 的非默认扩展。因此我没有使用多字节字符串函数。但是,数组键可能包含多字节字符集。为了支持这样的键,将需要一些字符串函数包装器的有条件实现,如果安装了适当的函数,则使用它们。带有 u
修饰符的 preg_*
函数可以作为支持多字节 unicode 键的替代方法。