Using the code below, I am getting divs with class 'post' from an external HTML file and print them to the posts page. Everything shown below works as it should be, however I would like to supplement the code with some automation. More precisely, instead of having hard coded values in the arrays I want to pass variable which were obtained by iterating through all divs with the needed class. Could you please help to implement such action. Thanks in advance.
PHP
<?php
$doc = new DOMDocument();
$doc->loadHTMLFile($url);
$xpath = new DOMXpath($doc);
$post = $xpath->query("//*[contains(@class, 'post')]");
$array = iterator_to_array($post);
?>
HTML posts page
<div><?php echo $doc->saveHTML($array[0]);?></div>
<div><?php echo $doc->saveHTML($array[1]);?></div>
<div><?php echo $doc->saveHTML($array[2]);?></div>
HTML external file
<div class="post">
post 1
</div>
<div class="post">
post 2
</div>
<div class="post">
post 3
</div>