I have a code:
<table>
<?php
if(isset($_SESSION['cart']))
foreach($_SESSION['cart'] as $key => $item){
?>
<tr>
<td><?php echo $item['num'] ?></td>
<td><?php echo $item['name'] ?></td>
<td>
<img class="cart_remove" src="images/x.png" alt="" /><span class="item_index"><?php echo $key ?></span>
</td>
</tr>
<?php } ?>
</table>
It prints session variables like:
1 name1
2 name2
3 name3
4 name4
But I need the latest added values print first. Like:
4 name4
3 name3
2 name2
1 name1
So, how can i print session variables in descending order? And also, is it possible to print, for example, latest 3 session variables?
Reverse the elements order:
If you want to print the last 3 entries in
$_SESSION['cart']specifically – you can do something like this: