You can make use of that array_filter result itself. You get all values that aren't NULL. So get the last key from that array and that is your end limit for array_slice function.
<?php
$arr = [null, null, 1, 4, 6, null, null, null, null,null];
$filtered_arr = array_filter($arr);
end($filtered_arr);
$limit = key($filtered_arr) + 1;
$arr = array_slice($arr,0,$limit);
print_r($arr);
Demo: https://3v4l.org/RVcjt
Update:
If the input array contains a 0, above answer could lead to incorrect results.
To rectify the same, we can filter out only NULL values from the array.
<?php
$arr = [null, null, 1, 4, 6, null, 0, null, null,null];
$filtered_arr = array_filter($arr,function($value){
return !is_null($value);
});
end($filtered_arr);
$limit = key($filtered_arr) + 1;
$arr = array_slice($arr,0,$limit);
print_r($arr);
Demo: https://3v4l.org/S5a96