php 在 foreach 中获取先前的数组值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4148501/
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 12:06:24  来源:igfitidea点击:

get previous array values in foreach

phparrays

提问by Henson

My array :

我的阵列:

$arr = array("jan","feb","mar","apr","mei","jun","jul","agu","sep","okt","nov","des");

then i do a foreach

然后我做一个 foreach

foreach($arr as $ar){
  echo $ar;
}

that will output jan to des

这将输出 jan 到 des

my question is how do i display the previous values in current key?

我的问题是如何显示当前键中的先前值?

For example, when I get to feb, I want to display jan too, when I get to jul, i want to display jun, etc.

例如,当我到达 feb 时,我也想显示 jan,当我到达 jul 时,我想显示 jun,等等。

回答by Matt Williamson

$previousValue = null;
foreach($arr as $ar){
  echo $ar;
  if($previousValue) {
    echo $previousValue;
  }
  $previousValue = $ar;
}

回答by Elzo Valugi

You can use the keys to get the previous key.

您可以使用密钥来获取上一个密钥。

foreach($arr as $key => $ar){
    $prev = $arr[$key-1];
    echo  "previous value -" .$prev;
}

You also have prev()as an internal array pointer:

您还可以将prev()作为内部数组指针:

$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport); // $mode = 'foot';
$mode = next($transport);    // $mode = 'bike';
$mode = next($transport);    // $mode = 'car';
$mode = prev($transport);    // $mode = 'bike';
$mode = end($transport);     // $mode = 'plane';

回答by bcosca

reset($array);
while($val=current($array))
{
    var_dump($val); // current
    var_dump(prev($array)); // previous
    next($array); // back to current
    next($array); // next
}

回答by Drew

foreach ($arr as $key => $ar) {
    //check we aren't on jan (otherwise we get $key = -1 which doesn't work)
    if ($key != 0) {
        //print previous month followed by current month
        echo $arr[$key - 1] . '-' . $ar . '<br />';
    }
}

//OR, if you want to be able to roll through years then:

$last_key = end(array_keys($arr));
foreach ($arr as $key => $ar) {
    //check we aren't on jan
    if ($key != 0) {
        //print previous month followed by current month
        echo $arr[$key - 1] . ' - ' . $ar . '<br />';
    } else {
        echo $arr[$last_key] . ' - ' . $ar . '<br />';
    }
}

回答by dazzafact

A little more Dynamic

多一点动态

$arr  = array(
    "jan",
    "feb",
    "mar",
    "apr",
    "mei",
    "jun",
    "jul",
    "agu",
    "sep",
    "okt",
    "nov",
    "des"
);
$arr2 = $arr;
foreach ($arr as $k => $currVal) {
    unset($arr2[$k]);
    foreach ($arr2 as $k => $v) {
        $nextVal = $v;

        break;
    }
    echo "next val: " . $nextVal;
    echo "current val: " . $currVal;
}

回答by Thilo

foreach($arr as $key => $value){
  if ($key > 0) {
   echo $arr[$key-1];
  }
  echo $value;
}

See this question and answer.

看到这个问题和答案

回答by dstarh

for ( $i = 0; $i <count($arr); $i++) {
    echo $arr[$i]
    if($i > 0){
     echo $arr[$i-1]
   }
}