Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to use paginator for a large array?

Have you checked here? Phalcon\Paginator\Adapter\NativeArray is what you need.

How big is that array?

Can contain thousands of records.



62.8k
Accepted
answer

Should not be a problem at all. Here is a quick example with 100k elements in array.

$data = [];
for ($i = 0; $i < 100000; ++$i) {
    $data[] = [
        'id' => $i,
        'title' => 'Sample title #'. $i,
        'date' => date('Y-m-d')
    ];
}
$paginator = new \Phalcon\Paginator\Adapter\NativeArray([
    'data' => $data,
    'limit' => 5,
    'page' => $this->request->getQuery('page', 'int', 1)
]);

d($paginator->getPaginate());

Result:

stdClass Object
(
    [items] => Array
        (
            [0] => Array
                (
                    [id] => 0
                    [title] => Sample title #0
                    [date] => 2016-12-15
                )

            [1] => Array
                (
                    [id] => 1
                    [title] => Sample title #1
                    [date] => 2016-12-15
                )

            [2] => Array
                (
                    [id] => 2
                    [title] => Sample title #2
                    [date] => 2016-12-15
                )

            [3] => Array
                (
                    [id] => 3
                    [title] => Sample title #3
                    [date] => 2016-12-15
                )

            [4] => Array
                (
                    [id] => 4
                    [title] => Sample title #4
                    [date] => 2016-12-15
                )

        )

    [first] => 1
    [before] => 1
    [current] => 1
    [last] => 20000
    [next] => 2
    [total_pages] => 20000
    [total_items] => 100000
    [limit] => 5
)