Question: how to get last data from foreach , i want to save for last data to result , 

image for result :

**Result**

[![enter image description here](https://i.stack.imgur.com/4gENH.jpg)](https://i.stack.imgur.com/4gENH.jpg)

```
<?php
$s_kredit   = 0;
foreach ($ju as $j ) {
                $s_debet += $j->debet   - $j->kredit;
                $s_kredit += $j->kredit - $j->debet;
<tr>
<td class="center"><?php if(@$set_saldo->debet==0) { echo "-"; } else { echo number_format($s_debet,0,".","."); } ;?></td>
<td class="center"><?php if(@$set_saldo->kredit==0) { echo "-"; } else { echo number_format($s_kredit,0,".","."); } ;?></td>
</tr>

```

Answer: 
You should use [end](http://php.net/manual/en/function.end.php) function.

> 
> end — Set the internal pointer of an array to its last element
> 
> 
> 

```
$array=[3,4,5,8];    
echo end($array); // 8

```
Create another variable

```
<?php
$s_kredit   = 0;
$s_final   = 0;
foreach ($ju as $j ) {
                $s_debet += $j->debet   - $j->kredit;
                $s_kredit += $j->kredit - $j->debet;
                $s_final = $j->kredit;
<tr>
<td class="center"><?php if(@$set_saldo->debet==0) { echo "-"; } else { echo number_format($s_debet,0,".","."); } ;?></td>
<td class="center"><?php if(@$set_saldo->kredit==0) { echo "-"; } else { echo number_format($s_kredit,0,".","."); } ;?></td>
</tr>
}

// show $s_final after the loop

```