Get latest elements in foreach php
Possible duplicate:
Getting the last 5 elements of a php array
Hi I have around 11000 elements in an array and I only want to display the last 5 or 10 elements
$i = 0;
foreach ($collection as $product_all) {
if($i==2) break;
echo $product_all->getId();
$neew = Mage::getModel('catalog/product')->load($product_all->getId());
echo'<pre>';
print_r($neew);
$i++;
}
with this i got the first 2 items, how can i get only the last items
source to share
This is Magento, $collection
not an array, but an iterator. This means array functions like array_slice
don't work, but you can simulate foreach in reverse order:
end($collection);
while($current = current($collection)) {
// ... (see below)
prev($collection);
}
Inside the loop, you have to build your array from the last 5 elements and break down after they appear:
$lastFive[] = $current;
if (count($lastFive) == 5) break;
Edit: Now that we've solved your immediate problem, please tell us about the performance. It's a very bad idea to get 11000 items from the database into memory so that you can only use 5 or 10 of them. You should find the code where it $collection
loads and starts there. Most likely it is something like:
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->setOrder('id', 'asc')->load();
This can be changed to (back ORDER, add LIMIT):
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->setOrder('id', 'desc')->setPageSize(5)->load();
Voilà only the last 5 elements are loaded.
Even better, your code looks like you only want IDs and not actual models, so the whole thing can be optimized for:
$collection = Mage::getModel('catalog/product')->getCollection();
$ids = $collection->setOrder('id', 'desc')->setPageSize(5)->getAllIds();
foreach ($ids as $id) {
$product = Mage::getModel('catalog/product')->load($id);
// do what you want
}
source to share
Try array_slice php function.
If you want to store the key, you can pass true as the fourth argument:
array_slice($a, -5, 5, true);
source to share
If you want to use the latest X
elements:
$i = 0;
$totalCount = count($collection);
$itemsCount = 5;
$x = $totalCount - $itemsCount;
foreach ($collection as $product_all) {
if ($i < $x) {
$i++;
continue;
}
echo $product_all->getId();
$neew = Mage::getModel('catalog/product')->load($product_all->getId());
echo'<pre>';
print_r($neew);
$i++;
}
If you only want to use the last one you can use:
$lastItem = $collection->getLastItem();
But the best solution is sorting and limiting your collection by me. Why get 11000+ products (!) If you only want to use 5 or 10 of them? If you get products without sorting, they are ordered by attribute created_at
as I remember. You can just sort in descending order and limit X
.
source to share
try it
<?php
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
function foreach_last($array, $n, $func) {
reset($array);
end($array);
$i = 0;
$n = min($n, count($array)) - 1;
while ( $i ++ < $n )
prev($array);
$func(current($array), key($array));
while ( $v = next($array) ) {
$func($v, key($array));
}
}
function print_e($v, $k) {
printf("k: %s, v: %s\n", $k, $v);
}
foreach_last($array, 5, 'print_e');
source to share