We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



93.7k
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
)