在PHP中抓取内容后,我无法以正确的顺序回显

I'm trying to scrape a law text and echo the different parts in the same orders but with different html tags.

I use the Simple HTML Dom Parser PHP library to scrape some text from a law text. But my foreach loop don't put the chapters and articles in the right order. I just have first the Titles, then all the chapters and then all the articles... The right hierarchy is: One title, followed by one chapter, followed by several articles.

Could you tell me what's wrong?

Url text law (in French): Link

Scraper code:

include('simple_html_dom.php');

    //  We take the url we want to scrape

    $URL = 'https://www.legifrance.gouv.fr/affichTexte.do?cidTexte=JORFTEXT000033011065&dateTexte=20160821';

    //  Curl init
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec ($ch);

    curl_close($ch);

    //  We get the html
    $html = new simple_html_dom();
    $html->load($result);

    foreach($html->find('ul > li') as $key=>$title) {
        $item['title'] = $title->find('div.titreSection', 0)  ->plaintext;
        if(preg_match("/^Titre/",$item['title'])) {
            $resultat[] = "<h1>".$item['title']."</h1>";
        }
        if(preg_match("/^Chapitre/",$item['title'])) {
            $resultat[] = "<h2>".$item['title']."</h2>";
        }
    }

    // Find all article blocks
    foreach($html->find('div.article') as $key=>$ele) {
        $item['article-title'] =  $ele->find('div.titreArt', 0)  ->plaintext;
        $item['article-body'] =  $ele->find('div.corpsArt', 0)  ->plaintext;
        $item['article-body-plain'] =  $ele->find('div > p', 0) ->plaintext;    
            $resultat[] = "<h3 id='article-title".$key."'>".$item['article-title']."</h3>";
            $resultat[] = "<h4 id='article-body".$key."'>".$item['article-body']."</h4>";
            $resultat[] = "<h5 id='article-body-plain".$key."'>".$item['article-body-plain']."</h5>";
    } // end foreach articles
     include 'vue_scrap.php';
?>

View :

<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Scraper Loi</title>
  </head>
<body>
    <section>
        <h2>Résultats :</h2>
<?php
    foreach ($resultat as $result){
        echo $result;
    }
?>      
    </section>
    <footer>
    </footer>
</body>
</html>

Here is what I get from now:

Titre Ier : DISPOSITIONS RELATIVES AU STATUT DE LA MAGISTRATURE
Chapitre Ier : Dispositions relatives à la composition du corps judiciaire

Chapitre II : Dispositions relatives au recrutement et à la formation professionnelle
Chapitre III : Dispositions relatives aux conditions de nomination
Chapitre IV : Dispositions relatives aux droits et obligations des magistrats
Chapitre V : Dispositions relatives aux autres modalités de recrutement des magistrats
Titre II : DISPOSITIONS RELATIVES AU CONSEIL SUPÉRIEUR DE LA MAGISTRATURE
Titre III : DISPOSITIONS DIVERSES ET TRANSITOIRES
Article 1

A modifié les dispositions suivantes :

Article 2

A modifié les dispositions suivantes :

Article 3

A modifié les dispositions suivantes :

Article 4

A modifié les dispositions suivantes :

Article 5

A modifié les dispositions suivantes :

Article 6

A modifié les dispositions suivantes :

Article 7

A modifié les dispositions suivantes :

Article 8

A modifié les dispositions suivantes :

Article 9

A modifié les dispositions suivantes :

Article 10

A modifié les dispositions suivantes :

...

Thank you for your help.