You need a loop within a loop – loop through a counter, and for each value, check for the existence of each key with the current counter value appended.
// max custom field index
$number = 40;
// the counter
$counter="";
// the meta keys to check for
$keys = array(
'custom_text',
'custom_image',
'custom_video'
);
// all our custom field values
$custom_fields = get_post_custom( get_the_ID() );
// loop over our counter
while( $counter < $number ){
// loop over each of the keys
foreach( $keys as $key ){
// check if a custom field with key + counter exists
if( isset( $custom_fields[ $key . $counter ] ) ){
// output the field
// values will be in an array, 0 is the first index.
// you can loop over these as well if you have multiple values.
echo $custom_fields[ $key . $counter ][0];
}
}
// increment the counter
$counter++;
}