移至前一个条目,使用ArrayIterator?

huangapple go评论58阅读模式
英文:

Move to previous entry with ArrayIterator?

问题

// 我想要实现的
$list->previous();
echo $list->current(); // 'page1'
英文:
$list = new ArrayIterator(array('page1', 'page2', 'page3', 'page4'));
echo $list->current(); // 'page1'
$list->next();
echo $list->current(); // 'page2'

// What I want to achieve
$list->previous();
echo $list->current(); // 'page1'

From there, how can I get last element page1 ?

答案1

得分: 0

你可以使用 seek() 方法和关键索引来将内部指针移动到最后一个元素。

$list->seek($list->key() - 1);

完整示例

$list = new ArrayIterator(array('page1', 'page2', 'page3', 'page4'));
echo $list->current(); // 'page1'
$list->next();
echo $list->current(); // 'page2'

$list->seek($list->key() - 1);
echo $list->current(); // 'page1'
英文:

You can use seek() method and key index to move internal pointer to last element

$list->seek($list->key() - 1);

Full example

$list = new ArrayIterator(array('page1', 'page2', 'page3', 'page4'));
echo $list->current(); // 'page1'
$list->next();
echo $list->current(); // 'page2'

$list->seek($list->key() - 1);
echo $list->current(); // 'page1'

答案2

得分: 0

I had this problem with an associative ArrayIterator, and this works both for simple and associative arrays, if you track the numeric position of the pointer as you cycle through the array ...

$list = new ArrayIterator(
[
'foo' => 'page1',
'bar' => 'page2',
'baz' => 'page3',
'qux' => 'page4',
'fred' => 'page5'
]
);
$numericIndex = -1;
foreach ($list as $key => $value) {
$numericIndex++;
var_dump($list->current());
if ($key === 'bar') {
// Jump ahead to do stuff to the following elements
$list->next();
var_dump('Jumped ahead to: ' . $list->current());
$list->next();
var_dump('Jumped ahead to: ' . $list->current());

// Rewind to original position to continue the loop
$list->seek($numericIndex);
}
}

That outputs:

...:string 'page1' (length=5)
...:string 'page2' (length=5)
...:string 'Jumped ahead to: page3' (length=22)
...:string 'Jumped ahead to: page4' (length=22)
...:string 'page3' (length=5)
...:string 'page4' (length=5)
...:string 'page5' (length=5)

英文:

I had this problem with an associative ArrayIterator, and this works both for simple and associative arrays, if you track the numeric position of the pointer as you cycle through the array ...

$list = new ArrayIterator(
    [
        'foo'  => 'page1',
        'bar'  => 'page2',
        'baz'  => 'page3',
        'qux'  => 'page4',
        'fred' => 'page5'
    ]
);
$numericIndex = -1;
foreach ($list as $key => $value) {
    $numericIndex++;
    var_dump($list->current());
    if ($key === 'bar') {
        // Jump ahead to do stuff to the following elements
        $list->next();
        var_dump('Jumped ahead to: ' . $list->current());
        $list->next();
        var_dump('Jumped ahead to: ' . $list->current());

        // Rewind to original position to continue the loop 
        $list->seek($numericIndex);
    }
}

That outputs:

...:string 'page1' (length=5)
...:string 'page2' (length=5)
...:string 'Jumped ahead to: page3' (length=22)
...:string 'Jumped ahead to: page4' (length=22)
...:string 'page3' (length=5)
...:string 'page4' (length=5)
...:string 'page5' (length=5)

huangapple
  • 本文由 发表于 2023年2月14日 01:17:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定