我有一个foreach循环,我想查看循环中是否有下一个元素,以便可以将当前元素与下一个元素进行比较。我怎样才能做到这一点?我已阅读了当前和下一个功能,但我无法弄清楚如何使用它们。获取foreach循环中的下一个元素
预先感谢
我有一个foreach循环,我想查看循环中是否有下一个元素,以便可以将当前元素与下一个元素进行比较。我怎样才能做到这一点?我已阅读了当前和下一个功能,但我无法弄清楚如何使用它们。获取foreach循环中的下一个元素
预先感谢
的唯一方法是将扭转阵列和然后循环。这将非数字索引数组以及工作:
$items = array(
'one' => 'two',
'two' => 'two',
'three' => 'three'
);
$backwards = array_reverse($items);
$last_item = NULL;
foreach ($backwards as $current_item) {
if ($last_item === $current_item) {
// they match
}
$last_item = $current_item;
}
如果你仍然有兴趣使用的current
和next
功能,你可以这样做:
$items = array('two', 'two', 'three');
$length = count($items);
for($i = 0; $i < $length - 1; ++$i) {
if (current($items) === next($items)) {
// they match
}
}
#2可能是最好的解。请注意,$i < $length - 1;
将在比较数组中的最后两项后停止循环。我把这个放在循环中,以便在这个例子中明确。你应该才算$length = count($items) - 1;
如果索引AR continious:
foreach($arr as $key => $val){
if(isset($arr[$key+1])){
echo $arr[$key+1]; // next element
}else{
//end of array reached
}
}
这不是真的,试试:array(1 =>'a',0 =>'b',100 =>'c'); – 2014-10-10 05:35:28
@EduardoRomero是的,这就是为什么我提到:'如果索引是连续的' – 2014-10-10 10:37:35
如果数字索引:
foreach ($foo as $key=>$var){
if($var==$foo[$key+1]){
echo 'current and next var are the same';
}
}
你可以得到数组的键在foreach前,然后用一个计数器来检查下一个元素,是这样的:
//$arr is the array you wish to cycle through
$keys = array_keys($arr);
$num_keys = count($keys);
$i = 1;
foreach ($arr as $a)
{
if ($i < $num_keys && $arr[$keys[$i]] == $a)
{
// we have a match
}
$i++;
}
这两个简单的阵列工作,如array(1,2,3)
和键控阵列,如array('first'=>1, 'second'=>2, 'thrid'=>3)
。
作为php.net/foreach指出:
除非阵列引用,foreach指定数组的一个拷贝,而不是在阵列本身进行操作。 foreach对数组指针有一些副作用。不要在foreach期间或之后依赖数组指针而不重置它。
换句话说 - 做你要做的事不是一个好主意。也许与某人谈论你为什么试图这样做会是一个好主意,看看是否有更好的解决方案?如果您没有任何其他可用资源,请随时在irc.freenode.net上的## PHP中提问。
你也许可以使用while循环代替的foreach:
while ($current = current($array))
{
$next = next($array);
if (false !== $next && $next == $current)
{
//do something with $current
}
}
我觉得这有帮助......谢谢! – 2015-07-30 18:46:38
请记住,在此之前,您可能必须“重置($ array)”,因为指针可能未设置为第一个元素。 – Jonathan 2015-10-16 10:39:01
foreach循环在PHP会遍历原来的数组的副本,使next()
和prev()
功能无用。如果你有一个关联数组,并需要获取下一个项目,你可以在数组键循环,而不是:
foreach (array_keys($items) as $index => $key) {
// first, get current item
$item = $items[$key];
// now get next item in array
$next = $items[array_keys($items)[$index + 1]];
}
由于计算出的排列键的具有连续索引本身,您可以使用,而不是访问原始数组。
注意这$next
将null
最后一次迭代,因为是在最后没有下一个项目。访问不存在的数组键会抛出一个PHP通知。为了避免这种情况,可以:
$next
index + 1
键与array_key_exists()
使用方法2完整的foreach可能看起来像这样存在:
foreach (array_keys($items) as $index => $key) {
// first, get current item
$item = $items[$key];
// now get next item in array
$next = null;
if (array_key_exists($index + 1, array_keys($items))) {
$next = $items[array_keys($items)[$index + 1]];
}
}
在每次迭代中使用array_keys?这必须是最慢的解决方案。 – Semra 2017-07-22 11:04:50
一般的解决方案可能是一个缓存迭代器。正确实现的缓存迭代器可以与任何Iterator一起使用,并可节省内存。 PHP SPL有一个CachingIterator,但它很奇怪,并且功能非常有限。但是,您可以编写自己的lookahead迭代器,如下所示:
<?php
class NeighborIterator implements Iterator
{
protected $oInnerIterator;
protected $hasPrevious = false;
protected $previous = null;
protected $previousKey = null;
protected $hasCurrent = false;
protected $current = null;
protected $currentKey = null;
protected $hasNext = false;
protected $next = null;
protected $nextKey = null;
public function __construct(Iterator $oInnerIterator)
{
$this->oInnerIterator = $oInnerIterator;
}
public function current()
{
return $this->current;
}
public function key()
{
return $this->currentKey;
}
public function next()
{
if ($this->hasCurrent) {
$this->hasPrevious = true;
$this->previous = $this->current;
$this->previousKey = $this->currentKey;
$this->hasCurrent = $this->hasNext;
$this->current = $this->next;
$this->currentKey = $this->nextKey;
if ($this->hasNext) {
$this->oInnerIterator->next();
$this->hasNext = $this->oInnerIterator->valid();
if ($this->hasNext) {
$this->next = $this->oInnerIterator->current();
$this->nextKey = $this->oInnerIterator->key();
} else {
$this->next = null;
$this->nextKey = null;
}
}
}
}
public function rewind()
{
$this->hasPrevious = false;
$this->previous = null;
$this->previousKey = null;
$this->oInnerIterator->rewind();
$this->hasCurrent = $this->oInnerIterator->valid();
if ($this->hasCurrent) {
$this->current = $this->oInnerIterator->current();
$this->currentKey = $this->oInnerIterator->key();
$this->oInnerIterator->next();
$this->hasNext = $this->oInnerIterator->valid();
if ($this->hasNext) {
$this->next = $this->oInnerIterator->current();
$this->nextKey = $this->oInnerIterator->key();
} else {
$this->next = null;
$this->nextKey = null;
}
} else {
$this->current = null;
$this->currentKey = null;
$this->hasNext = false;
$this->next = null;
$this->nextKey = null;
}
}
public function valid()
{
return $this->hasCurrent;
}
public function hasNext()
{
return $this->hasNext;
}
public function getNext()
{
return $this->next;
}
public function getNextKey()
{
return $this->nextKey;
}
public function hasPrevious()
{
return $this->hasPrevious;
}
public function getPrevious()
{
return $this->previous;
}
public function getPreviousKey()
{
return $this->previousKey;
}
}
header("Content-type: text/plain; charset=utf-8");
$arr = [
"a" => "alma",
"b" => "banan",
"c" => "cseresznye",
"d" => "dio",
"e" => "eper",
];
$oNeighborIterator = new NeighborIterator(new ArrayIterator($arr));
foreach ($oNeighborIterator as $key => $value) {
// you can get previous and next values:
if (!$oNeighborIterator->hasPrevious()) {
echo "{FIRST}\n";
}
echo $oNeighborIterator->getPreviousKey() . " => " . $oNeighborIterator->getPrevious() . " -----> ";
echo "[ " . $key . " => " . $value . " ] -----> ";
echo $oNeighborIterator->getNextKey() . " => " . $oNeighborIterator->getNext() . "\n";
if (!$oNeighborIterator->hasNext()) {
echo "{LAST}\n";
}
}
从元素#2开始并将其与元素#1进行比较是否可以接受? – 2011-02-23 21:01:50
不,它必须从1开始 – chchrist 2011-02-23 21:54:35