PHP保留数组中的前6个项目并删除其余项目

tma*_*314 0 php

我想知道如何删除任何数字项后面的特定数字,如6.你有什么东西在PHP中可以做到吗?或者它是一个需要编写的自定义函数

Joh*_*ker 5

您可以使用array_slice来实现此目的.例如:

$testArray = range(0, 10);

// Ensure there are at least six items in the source array.
if(count($testArray) >= 6) {
   // Grab the first six items.
   $firstSixItemsFromArray = array_slice($testArray, 0, 6);
}
Run Code Online (Sandbox Code Playgroud)