PHP 数组在 foreach() 中获取下一个键/值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20479610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:14:12  来源:igfitidea点击:

PHP array get next key/value in foreach()

phparraysloopsforeach

提问by danielb

I am looking for a way to get the next and next+1 key/value pair in a foreach(). For example:

我正在寻找一种在 foreach() 中获取 next 和 next+1 键/值对的方法。例如:

$a = array('leg1'=>'LA', 'leg2'=>'NY', 'leg3'=>'NY', 'leg4'=>'FL');

foreach($a AS $k => $v){

    if($nextval == $v && $nextnextval == $v){
       //staying put for next two legs
    }

}

回答by Lajos Veres

You can't access that way the next and next-next values.

您不能以这种方式访问​​ next 和 next-next 值。

But you can do something similar:

但是你可以做类似的事情:

$a = array('leg1'=>'LA', 'leg2'=>'NY', 'leg3'=>'NY', 'leg4'=>'FL');

$keys = array_keys($a);
foreach(array_keys($keys) AS $k ){
    $this_value = $a[$keys[$k]];
    $nextval = $a[$keys[$k+1]];
    $nextnextval = $a[$keys[$k+2]];

    if($nextval == $this_value && $nextnextval == $this_value){
       //staying put for next two legs
    }
}

回答by Maciej Sz

I've found the solution with complexity O(n)and does not require seeking through array back and forward:

我找到了复杂的解决方案,O(n)不需要通过数组来回寻找:

$a = array('leg1'=>'LA', 'leg2'=>'NY', 'leg3'=>'NY', 'leg4'=>'FL');

// initiate the iterator for "next_val":
$nextIterator = new ArrayIterator($a);
$nextIterator->rewind();
$nextIterator->next(); // put the initial pointer to 2nd position

// initiaite another iterator for "next_next_val":    
$nextNextIterator = new ArrayIterator($a);
$nextNextIterator->rewind();
$nextNextIterator->next();
$nextNextIterator->next(); // put the initial pointer to 3rd position

foreach($a AS $k => $v){

    $next_val = $nextIterator->current();
    $next_next_val = $nextNextIterator->current();

    echo "Current: $v; next: $next_val; next_next: $next_next_val" . PHP_EOL;

    $nextIterator->next();
    $nextNextIterator->next();
}

Just remember to test for valid()if you plan to relay on the $next_valand $next_next_val.

只要记住测试valid()您是否打算在$next_val和上中继$next_next_val

回答by jszobody

Here's one way to do it:

这是一种方法:

while($current = current($a)) {
    $next = next($a);
    $nextnext = next($a);

    // Comparison logic here

    prev($a); // Because we moved the pointer ahead twice, lets back it up once
}

Example: http://3v4l.org/IGCXW

示例:http: //3v4l.org/IGCXW

Note that the loop written this way will never examine the last element in your original array. That could be fixed, although with your current logic it doesn't seem to matter since there are no "more" elements to compare the last one to.

请注意,以这种方式编写的循环永远不会检查原始数组中的最后一个元素。这可以解决,尽管按照您当前的逻辑,这似乎无关紧要,因为没有“更多”元素可以与最后一个元素进行比较。

回答by Dave

You can't simply "stay put" in a loop. I suspect you're looking to do something a lot easier than write custom iterators. If you simply want to ignore entries with duplicate keys, then track the last key and compare it to the current one.

你不能简单地“留在原地”在一个循环中。我怀疑您正在寻找比编写自定义迭代器更容易的事情。如果您只想忽略具有重复键的条目,请跟踪最后一个键并将其与当前键进行比较。

$a = array('leg1'=>'LA', 'leg2'=>'NY', 'leg3'=>'NY', 'leg4'=>'FL');

// Prints LA NY FL
$last_v = null;
foreach ( $a as $k => $v ){
    if ( $last_v == $v ) {
        /**
         *  Duplicate value, so skip it
         */
        continue;
    }
    echo $v.' ';
    $last_v = $v;
}

回答by Peter

Have a look at CachingIterator, as described in this answer:

看看 CachingIterator,如本答案所述:

Peek ahead when iterating an array in PHP

在 PHP 中迭代数组时提前查看

Or use array_keys() is in another answer posted for the same question, e.g.

或者使用 array_keys() 是在为同一问题发布的另一个答案中,例如

$keys = array_keys($array);
for ($i = 0; $i < count($keys); $i++) {
    $cur = $array[$keys[$i]];
    $next = $array[$keys[$i+1]];
}

回答by ern0

Funny, I'm programming PHP for a decade (with years pause), but I needed one-by-one walk functions just a week ago.

有趣的是,我编写 PHP 已经有十年了(停顿了几年),但就在一周前,我还需要一对一的步行功能。

Here you are: next, prev, reset etc. See "see also" section. Also, check array_keys()

你在这里:next, prev , reset 等等。参见“另见”部分。另外,检查array_keys()