18

My array :

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

then i do a foreach

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

that will output jan to 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.

7 Answers 7

58
$previousValue = null;
foreach($arr as $ar){
  echo $ar;
  if($previousValue) {
    echo $previousValue;
  }
  $previousValue = $ar;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This has the added advantage of working for arrays with string and non-sequential indices.
12

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:

$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';

3 Comments

isn't this supposed to display current and previous? also won't this die on the first iteration?
this is not happening inside a loop.. but it could happend. it is relative to the internal pointer of the current element
This only works if your keys are integers and in order
2
reset($array);
while($val=current($array))
{
    var_dump($val); // current
    var_dump(prev($array)); // previous
    next($array); // back to current
    next($array); // next
}

Comments

1
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 />';
    }
}

Comments

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

Comments

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

See this question and answer.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.