为每个数组元素计数字符串

时间:2018-11-07 08:44:23

标签: php

我正在尝试计算每个数组元素中的字符串。

 function pageContent(String $url): \DOMDocument
    {
        $html = cache()->rememberForever($url, function () use ($url) {
            return file_get_contents($url);
        });
        $parser = new \DOMDocument();
        libxml_use_internal_errors(true);

        $parser->loadHTML($html);
        libxml_use_internal_errors(false);

        return $parser;
    }


        $link[] = "https://sumai.tokyu-land.co.jp";
        $url = "https://sumai.tokyu-land.co.jp/osaka";
        $getContent = pageContent($url);
        $getXpath = new \DOMXPath($getContent);
        $getRoute = $getXpath->query("//ul[@class='p-articlelist-content-img']/li[1]/img");
        foreach ($getRoute as $photos){
             $photo[] = $photos->getAttribute('src');
             $link .= $photo;
             $allphoto[] = $link;
        }

阵列中有9张照片:像这样的/search/area/image/object_id/K4720000,我想将每个元素的头部添加到此行https://sumai.tokyu-land.co.jp。我尝试过,但是弹出了一些字符串对话错误数组

有什么解决办法吗?

1 个答案:

答案 0 :(得分:2)

您最后的代码是问题...

foreach ($getRoute as $photos){
     $photo[] = $photos->getAttribute('src');  // This creates photo as an array
     $link .= $photo;  // This then tries to add it as a string
     $allphoto[] = $link;
}

相反...

foreach ($getRoute as $photos){
     $allphoto[] = $link.$photos->getAttribute('src');
}

也改变

$link[] = "https://sumai.tokyu-land.co.jp";

因为这也是一个数组

$link = "https://sumai.tokyu-land.co.jp";