How can a foreach loop be effectively used to iterate over posts retrieved from an API in PHP and display them on a webpage?

When retrieving posts from an API in PHP, a foreach loop can be effectively used to iterate over the posts and display them on a webpage. The loop will go through each post, extract the necessary information (such as title and content), and output it in the desired format on the webpage.

// Assume $posts is an array of posts retrieved from the API
foreach ($posts as $post) {
    echo '<div>';
    echo '<h2>' . $post['title'] . '</h2>';
    echo '<p>' . $post['content'] . '</p>';
    echo '</div>';
}