php - How to get last value of for loop? -


i'm trying value of $row->price last iteration in loop below

foreach ($getprices->result() $row) {     if ($bb=='on'){         $pric = $row->price+2.50;         $pri = number_format($pric,2);     }else{         $pric = $row->price;         $pri = number_format($pric,2);     } 

i did try following, didn't appear work

$numitems = count($getprices->result()); $i = 0; foreach($getprices->result() $row) {   if(++$i === $numitems) {     if ($bb=='on'){         $pric = $row->price+2.50;         $pri = number_format($pric,2);     }else{         $pric = $row->price;         $pri = number_format($pric,2);     }   } }  

any suggestions?

use $row->price after loop has ended:

foreach ($getprices->result() $row) {     // ... }  $lastprice = $row->price; 

this trick, work. if iterating on array can this:

$array = $getprices->result(); foreach ($array $row) {     // ... }  $lastprice = end($array)->price; // work independently of loop 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -