2016-12-30 108 views
0

假設我有此數組:返回第一個X項:3

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 
    ) 

) 

我嘗試這樣的,但它不是那麼正確:

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

你有其他的想法?

結果應該是

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

這種情況需要impliment

+0

'$ part = array_slice($ lastviewedarticles,0,3);'? –

+0

是的,但如果我有100個元素的數組? –

+0

然後,你仍然可以使用'array_slice' –

回答

0

使用Array Slice

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