Skip to content

Instantly share code, notes, and snippets.

@o
Created December 22, 2010 14:08
Show Gist options
  • Save o/751548 to your computer and use it in GitHub Desktop.
Save o/751548 to your computer and use it in GitHub Desktop.
Sample of using PHP SPL Limit Iterator, you can use for the limiting cached objects
<?php
/**
* Creating dummy data for pagination
*/
$result = array();
for ($i = 0; $i < 100; $i++) {
$result[$i]['age'] = rand(18, 50);
$result[$i]['facebook_id'] = rand(1000000, 9999999);
}
/**
* Defining limit and offset
*/
$limit = 10;
$page = 5;
/**
* We will use Limit Iterator for filtering data
*/
foreach (new LimitIterator(new ArrayIterator($result), $page * $limit, $limit) as $key => $value) {
var_dump($key, $value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment