0

Ok so i have this foreach php loop

<?php foreach($final_products as $product){ ?>

and I am accessing it like this

<?php print $product['product_price']; ?>

which is great but the challenge I have is how do i access the next element in the array within this array so like this

<?php print $product['product_price'] + $product["next_product"]['product_price']; ?>

next_product is just a name i have to illustrate that i need the next product in the products arrays price but still continue with the flow of the array loop...any ideas

Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321
  • 2
    how is the array indexed? Is it a numeric array or an associative array? If it is a numeric array, then instead of using a foreach loop, use a for loop and then you can simply +1 to the current key to get the next. – Jonathan Kuhn Jun 08 '11 at 16:14
  • 2
    possible duplicate of [Peek ahead when iterating an array in PHP 5.2](http://stackoverflow.com/questions/2458099/peek-ahead-when-iterating-an-array-in-php-5-2) – Wrikken Jun 08 '11 at 16:21
  • 2
    ^ The CachingIterator seriously rocks :) – Wrikken Jun 08 '11 at 16:22

3 Answers3

2

Consider the situation where you're at the end of the loop, and there is no "next_product"; what do you want to do then?

Probably the best situation to do is to not be looking for the next product, but store the last product you processed, and work from there. That way, you're not getting ahead of your looping process.

Paul Sonier
  • 38,903
  • 3
  • 77
  • 117
  • **this has the same inverse problem**. What you do with the first element of the loop? You still need a checking for both solution. Someone with your reputation shouldn't make such errors – dynamic Jun 08 '11 at 16:15
  • 1
    that really isn't a problem at all if he uses a `for()` loop that begins with 0 and ends with n-2. @yes123, I agree. – Matthew Jun 08 '11 at 16:16
  • 1
    ...except it turns out he is using a non-indexed array. So skipping the first is going to be easier. – Matthew Jun 08 '11 at 16:25
  • @yes123: An iterator (foreach) implies sequential iteration across a collection of indefinite size; the size could be zero, or one, or more. When the elements of your collection have an interaction between them ("this" -> "next"), and your analysis process deals with sequential processing (foreach), you can only deal with previously examined values. – Paul Sonier Jun 08 '11 at 16:31
  • We are talking with array here, And if the keys are all sequential then you can easly check the next within a for. I may agree with you if the OP array keys aren't int sequential – dynamic Jun 08 '11 at 16:35
1
<?php 
  for($i=0;$i<count($final_products)-1;$i++){  
      echo $final_products[$i+1];
  }
?>

Use: $i + 1

I have added the for loop to be safe in the cast of the last element in array. Thanks @KonForce

If you need to something with the last elemnet of array yuo could use array_pop();

dynamic
  • 46,985
  • 55
  • 154
  • 231
1

You could start your loop one step ahead an use a temporary variable that holds the prevous item like this:

$curr = null;
foreach ($arr as $next) {
    if (!is_null($curr)) {
        // your code
    }
    $curr = $next;
}

With this the first iteration is skipped to assign $curr so in the next iteration $curr is actually the previous item.

Gumbo
  • 643,351
  • 109
  • 780
  • 844