简体   繁体   中英

Put array inside an array while in loop

Im working on a simple app that scans an array of websites, what I'm trying to do is save the urls in an array then put that in another array, my problem is only the result of the first domain on the array is being displayed(sorry my observation is wrong earlier).

    <?php

        $arrDomains = array('http://example1.com/', 'http://example2.com/');

        $arrExternals = array();
        for($i = 0; $i < count($arrDomains); $i++){

            $domain = test_input($arrDomains[$i]);

            $domain = filter_var($domain, FILTER_SANITIZE_URL);

            // START HERE

            $html = file_get_contents($domain);

            $dom = new DOMDocument();
            @$dom->loadHTML($html);

            // grab all the on the page
            $xpath = new DOMXPath($dom);
            $hrefs = $xpath->evaluate("/html/body//a");

            $external = array();

            for ($i = 0; $i < $hrefs->length; $i++) {
                $href = $hrefs->item($i);
                $url = $href->getAttribute('href');

                if (filter_var($url, FILTER_VALIDATE_URL) !== false) {

                    if (strpos($url, 'mailto') === false) { // exclude emails

                        if (!in_array($url, $external)) {
                            array_push($external, $url);
                        }
                    }
                }
            }

            array_push($arrExternals, $external);

        }


?>

You need to change variable $i because it overrides $i in the first for loop. I changed one $i to $j:

    $arrDomains = array('http://example1.com/', 'http://example2.com/');

    $arrExternals = array();
    for($i = 0; $i < count($arrDomains); $i++){

        $domain = test_input($arrDomains[$i]);

        $domain = filter_var($domain, FILTER_SANITIZE_URL);

        // START HERE

        $html = file_get_contents($domain);

        $dom = new DOMDocument();
        @$dom->loadHTML($html);

        // grab all the on the page
        $xpath = new DOMXPath($dom);
        $hrefs = $xpath->evaluate("/html/body//a");

        $external = array();

        for ($j = 0; $j < $hrefs->length; $j++) {
            $href = $hrefs->item($j);
            $url = $href->getAttribute('href');

            if (filter_var($url, FILTER_VALIDATE_URL) !== false) {

                if (strpos($url, 'mailto') === false) { // exclude emails

                    if (!in_array($url, $external)) {
                        array_push($external, $url);
                    }
                }
            }
        }

        array_push($arrExternals, $external);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM