How to get post image caption by index

I have used function to extract data inside figcaption tags, and $position is stand for the index of the current image.

  function getImageCaption($postID, $position){
        $post_id = $postID;
        // get the post object
        $post = get_post( $post_id );
        // we need just the content
        $content = $post->post_content;
        // we need a expression to match things
        $regex = '/<figcaption>(.*)<\/figcaption>/';
        // we want all matches
        preg_match_all( $regex, $content, $matches );
        // reversing the matches array
        $matches = array_reverse($matches);
        // we've reversed the array, so index 0 returns the result
        foreach ($matches as $key => $value) {
            return $value[$position];
        }
    }