Q:

How to paginate array

My code :
    <?php

$array = json_decode('{ "friends": { "data": [ { "id": "1000002470615", "gender": "female" }, { "id": "1000005198891", "gender": "female" }, { "id": "1000007859390", "gender": "female" }, { "id": "1000008308250", "gender": "female" }, { "id": "1000009416380", "gender": "male" }, { "id": "1000010894609", "gender": "female" }, { "id": "1000010991284", "gender": "male" }, { "id": "1000011095914", "gender": "female" }, { "id": "1000014648465", "gender": "female" }, { "id": "1000017041889", "gender": "female" }, { "id": "1000017519651", "gender": "female" }, { "id": "1000019029610", "gender": "female" }, { "id": "1000019497511", "gender": "female" }, { "id": "1000019681751", "gender": "female" }, { "id": "1000020582845", "gender": "male" }, { "id": "1000021389766", "gender": "male" }, { "id": "1000022247272", "gender": "female" }, { "id": "1000025425217", "gender": "female" }, { "id": "1000026359294", "gender": "female" }, { "id": "1000031258301", "gender": "male" }, { "id": "1000031499108", "gender": "female" }, { "id": "1000032349067", "gender": "male" }, { "id": "1000032428345", "gender": "female" }, { "id": "1000033101313", "gender": "male" }, { "id": "1000033288824", "gender": "female" }, { "id": "1000033525527", "gender": "female" }, { "id": "1000034062687", "gender": "male" }, { "id": "1000035232214", "gender": "female" }, { "id": "1000035804034", "gender": "female" }, { "id": "1000040066742", "gender": "male" }, { "id": "1000041457943", "gender": "female" }, { "id": "1000042302703", "gender": "female" }, { "id": "1000043758095", "gender": "female" }, { "id": "1000045378342", "gender": "male" }, { "id": "1000048531666", "gender": "female" }, { "id": "1000049731704", "gender": "female" }, { "id": "1000050719295", "gender": "female" }, { "id": "1000050736082", "gender": "male" }, { "id": "1000051733891", "gender": "female" }, { "id": "1000052976810", "gender": "male" }, { "id": "1000053432719", "gender": "male" }, { "id": "1000054595524", "gender": "male" }, { "id": "1000055567339", "gender": "female" }, { "id": "1000056180174", "gender": "female" }, { "id": "1000056737207", "gender": "male" }, { "id": "1000058322123", "gender": "male" }, { "id": "1000058592478", "gender": "female" }, { "id": "1000059646724", "gender": "male" }, { "id": "1000062848532", "gender": "female" }, { "id": "1000062994936", "gender": "female" }, { "id": "1000066441440", "gender": "male" } ] } }', true);

foreach($array['friends']['data'] as  $details) {
    echo $details['id'], '<br />';
    }

?>

result:
1000002470615
1000005198891
1000007859390
1000008308250
1000009416380
1000010894609
1000010991284
1000011095914
1000014648465
1000017041889
1000017519651
1000019029610
1000019497511
...

How to define my paginate as in the example below?
    1000002470615
    1000005198891
    1000007859390
    1000008308250
    1000009416380
<--Previous Next--> 

What should you use to make this possible.
Sorry for my English expression.

A:

I would do it like this :    
$items = 5; // 5 items per page
$page = $_GET['page']; // current page

$array = json_decode('...', true);
$friends = $array['friends']['data'];

$pages = ceil(count($friends) / $items); // number of pages (total)
$page = $page < 1 ? 1 : $page; // if page is inferior to 1 make it 1
$page = $page > $pages ? $pages : $page; // if is superior to total make it total
$current = array_slice(array_slice($friends, $page-1), 0, $items);

// show friends for current page
foreach($current as $friend) {
    echo $friend['id'] . '<br />';
}

// pagination
for($i = 1; $i <= $pages; $i++) {
    $class = $i == $page ? ' class="active"' : '';
    echo '<a' . $class . ' href="url?page=' . $i . '">' . $i . '</a>';
}

