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
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...- array_shift edits the array in place and returns the item it removed
精彩评论