Вариант с foreach:
$data = ['row1', 'row2', 'row3', 'row4', 'row5'];
$countToDelete = 3;
$i = 0;
foreach ($data as $index => $value) {
unset($data[$index]);
if ($i >= $countToDelete - 1) {
break;
}
$i++;
}
var_dump($data);
/*
array(2) {
[3]=>
string(4) "row4"
[4]=>
string(4) "row5"
}
*/
Вариант с
array_filter:
$data = ['row1', 'row2', 'row3', 'row4', 'row5'];
$countToDelete = 3;
$filtered = array_filter($data, function ($value) use (&$countToDelete) {
return $countToDelete-- <= 0;
});
var_dump($filtered);
/*
array(2) {
[3]=>
string(4) "row4"
[4]=>
string(4) "row5"
}
*/