1

I've this kind of array

Array
(
[12-12] => 9
[01-13] => 10
[02-13] => 11
[03-13] => 14
[05-13] => 16
[09-13] => 17
)

with a simple

 foreach ($arr as $key=>$value)

i can access every key and element.

But i need to get, from second element, also previous element:

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

I need so to print a similar thing:

12-12 => 0 => 9
01-13 => 9 => 10
02-13 => 10 => 11

and so on

"Obviously" it doesn't function, because of string key. Any help? Thank you!

1
  • Why not store the current key in a variable outside the loop and change it at each iteration to the current key after you're done using it? Commented Sep 6, 2013 at 18:57

5 Answers 5

4
$previous_value = 0;
foreach ($arr as $key=>$value) {
    echo $key .'=>'. $previous_value .' '.  $value;
    $previous_value = $value;
}

This should solve it.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Solved! Thank you very much to you and to others answering!
2

Many have talked about just storing the previous key, which I think is correct; but, for the sake of variety, you can also use array_keys, like so:

$keys=array_keys($arr);
$i=0;
foreach ($arr as $key=>$value) {
  if ($i==0) {
    echo $key .'=> 0 '.  $value;
  } else {
    echo $key .'=>'. $arr[$keys[$i-1]] .'=>'. $value;
  }
 $i++
}

See, $keys will contain an array with only the keys of $arr, so referring to $keys[$i] is the same as printing $key for record $i in a foreach loop. Sorry if I got it wrong, I think that's what you were trying to do, I hope.

Cheers.

1 Comment

i.e. this answer is more close to my "philosphy" than other answers. So, next time i'll try this, btw you did understand exactly what i need (I'm referring to your "Sorry if I got it wrong, I think that's what you were trying to do, I hope."
1

Just save the previous key.

    $prevKey = null;
    foreach ($arr as $key=>$value) {
      if ($i==1) {
        echo $key .'=> 0 '.  $value;
      } else {
        echo $key .'=>'. $arr[$prevKey] .'=>'. $value;
      }
    $prevKey = $key;

    }

Comments

1

You had the right idea, just wrong usage :

$prev = null;
foreach ($arr as $key=>$value) {
    if (is_array($prev)) {
        print_r($prev)
        echo $key .'=> 0 '.  $value;
    } else {
        echo $key .'=>'. $arr[$key-1] .'=>'. $value;
    }
    $prev = array($key, $value);
}

Comments

1

Save the previous value in each iteration

$prevValue = 0;
foreach ($arr as $value) {
    echo $key . '=>' . $prevValue . '=>' . $value;
    $prevValue = $value;
}

1 Comment

Perfect. Solved! Thank you very much to you and to others answering!

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.