Returning first x items from array : 3

42 views Asked by At

Suppose I have this array :

Array
(
[0] => Array
    (
        [id] => 100828698
        [token] => 123
    )

[1] => Array
    (
        [id] => 100828698
        [token] => fdsfsdfsd
    )

[2] => Array
    (
        [id] => 100829014
        [token] => oidshiufjsd
    )

[3] => Array
    (
        [id] => 100829014
        [token] => sdjfdhskjfdsh
    )

)

I try like this, but it is not so properly :

$count = count($lastviewedarticles);
if($count>=3)
    array_shift($lastviewedarticles);
    $lastviewedarticles[] = $articleid;
}

Do you have other ideas?

The result should be

1. If count(array) > 3  
2. Make a call of db
3. Fetch first 3 elements
4. Get next 3 elements
etc...

This situation a need to impliment

1

There are 1 answers

0
Lucas Meine On

Use Array Slice.

<?php
 $numberOfItems = 3 // How many items would you like to select?
 $first_items = array_slice(lastviewedarticles, 0, $numberOfItems);
 return $first_items;