开发者

Getting last 6 values from an multidimensional array

I have a need to get the last 6 values from an multidimentional array, I was trying to do something like this

for($i=0;$i<6;$i++){
    $stats = array_shift($stats);
}

But then after the first array_shift I get the following error

PHP Warning: array_shift(): The argument should be an array

Are there any functions that could do this开发者_JS百科 in PHP?


You can use array_slice():

$stats = array_slice($stats, -6);

The reason your code isn't working is because

  1. array_shift() removes from the front of the array - so you'd end up with the first 6 removed, which is not the same as getting the last 6 unless your array has 12 items...
  2. array_shift edits the array in place and returns the item it removed
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜