Array ids post to function have_post

If I understand it properly,

You can use the post__in parameter to query for specific posts where the post ID is in that parameter:

$ids = [ 1, 2, 3, 4, 5 ];
$query = new WP_Query( array(
    'post__in' => $ids,
    // ... other args, if any.
) );

// Then loop through the posts.
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) : $query->the_post();
        the_title();
        // ... your code here.
    endwhile;
    wp_reset_postdata();
}

Alternatively, use foreach with setup_postdata() without having to do the new WP_Query():

global $post;
$ids = [ 1, 2, 3, 4, 5 ];
foreach ( $ids as $id ) {
    $post = get_post( $id );
    setup_postdata( $post );

    the_title();
    // ... your code here.
}
wp_reset_postdata();