0

I have an array serving multiple purpose, sometimes I use it for displaying all records, sometimes I use it for displaying the first record, sometimes I use it for displaying the last record.

For displaying all records, I can use

<?php foreach($array['products'] as $product) { ?>
  <li>Name: <?php echo $product['name']; ?>'/></li>
<?php } ?>

For displaying the first record, I can use

<?php $product=reset($array['products']); ?>
  <li>Name: <?php echo $product['name']; ?>'/></li>

For displaying the last record, what should I do?

5 Answers 5

7

Use end,

<?php $product=end($array['products']); ?>
  <li>Name: <?php echo $product['name']; ?>'/></li>
Sign up to request clarification or add additional context in comments.

Comments

0
<?php echo $product[count($product)]['Name']; ?>

Comments

0

The function end() will do this. It moves the pointer to the end, and returns the last element.

Comments

0

try this

$products=array_reverse($products);

<?php $product=reset($array['products']); ?>
  <li>Name: <?php echo $product['name']; ?>'/></li>

1 Comment

better way is given by Rikesh.
0

Use end, this will forward your array to the end and return the pointer.

echo end($products);

Comments

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.