I have a feeling the answer to your question is going to be “use [FPHP]glob/FPHP instead”, but like iamjones, i’m curious how you’re actually filtering.
It looks like you are wanting to use something like array_slice() to get a limited number of files from the full array.
$chosen_pics = array_slice($pics, 0, 50);
foreach ($chosen_pics as $item) {
Bear in mind that this will include directories (even the . and .. ones), so it’s also a good idea to filter the array. One way to get only the files from the array would be to use array_filter() with [url=http://php.net/is_file]is_file().