PHP数组获取foreach()中的下一个键/值[duplicate]

时间:2022-02-15 22:09:20

This question already has an answer here:

这个问题已经有了答案:

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

我正在寻找在foreach()中获取下一个和下一个+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
    }

}

6 个解决方案

#1


8  

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

您不能以这种方式访问下一个和下一个值。

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
    }
}

#2


2  

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_val and $next_next_val.

如果您打算在$next_val和$next_next_val上进行传递,请记住测试有效的()。

#3


1  

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.

注意,以这种方式编写的循环将永远不会检查原始数组中的最后一个元素。这是可以修正的,尽管根据你目前的逻辑,这似乎并不重要,因为没有“更多”的元素可以与上一个进行比较。

#4


0  

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]];
}

#5


0  

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;
}

#6


-2  

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

有趣的是,我编程PHP已经10年了(有几年的时间),但仅仅一周前我还需要一个单独的行走功能。

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

这里有:下一个,prev, reset等。参见“See also”部分。同时,检查中的()

#1


8  

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

您不能以这种方式访问下一个和下一个值。

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
    }
}

#2


2  

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_val and $next_next_val.

如果您打算在$next_val和$next_next_val上进行传递,请记住测试有效的()。

#3


1  

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.

注意,以这种方式编写的循环将永远不会检查原始数组中的最后一个元素。这是可以修正的,尽管根据你目前的逻辑,这似乎并不重要,因为没有“更多”的元素可以与上一个进行比较。

#4


0  

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]];
}

#5


0  

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;
}

#6


-2  

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

有趣的是,我编程PHP已经10年了(有几年的时间),但仅仅一周前我还需要一个单独的行走功能。

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

这里有:下一个,prev, reset等。参见“See also”部分。同时,检查中的()