在foreach中获取先前的数组值 [英] get previous array values in foreach
本文介绍了在foreach中获取先前的数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数组:
$arr = array("jan","feb","mar","apr","mei","jun","jul","agu","sep","okt","nov","des");
然后我做个foreach
then i do a foreach
foreach($arr as $ar){
echo $ar;
}
将jan输出到des
that will output jan to des
我的问题是如何显示当前键中的先前值?
my question is how do i display the previous values in current key?
例如,当我进入二月时,我也想显示jan,当我进入七月时,我想显示jun等.
For example, when I get to feb, I want to display jan too, when I get to jul, i want to display jun, etc.
推荐答案
$previousValue = null;
foreach($arr as $ar){
echo $ar;
if($previousValue) {
echo $previousValue;
}
$previousValue = $ar;
}
这篇关于在foreach中获取先前的数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文