如何在一个数组中推送每个函数的所有值 - PHP DomCrawler

问题描述 投票:0回答:1

这是我的尝试:

function getData($data)
{
    $params   = [];
    $products = [];
    $crawler  = new Crawler($data);
    // $last_updated= $crawler->filter('#content > div.listing__ContentWrapper-sc-1a1m3sv-1.cIZBes.sc-ksYbfQ.kiNQqU > div > div:nth-child(2) > div > div:nth-child(2) > div.sc-ksYbfQ.gbxQgD > div.sc-bdVaJa.styled-components__ListingCategoryHeader-sc-45yec-7.goZdqJ.sc-ksYbfQ.iNGdR > div > span > span')->text();
    // $params['last_updated']= $last_updated;
    // echo $last_updated;
    $products = $crawler->filter('#content [class*=ListingCategoryHeader] + ol')
        ->each(function (Crawler $node) {
            $node->filter('li')->each(function (Crawler $node) {
                $image = $node->filter('img')->attr('src');
                $node->filter('[class*=styled-components__MenuDetailsContainer]')
                    ->each(function (Crawler $node) {
                        $name                  = $node->filter('div [class*=styled-components__Name-sc]');
                        $params['name']        = $this->validate->getText($name);
                        $category              = $node->filter('div [class*=components__BrandCategory]');
                        $getCategory           = $this->validate->getText($category);
                        $modifyCategory        = $this->modify->modifyCategoryText($getCategory);
                        $params['category']    = $modifyCategory;
                        $modifyStrain          = $this->modify->modifyStrainText($getCategory);
                        $params['strain_type'] = $modifyStrain;
                        $prices                = $node->filter('[class*=components__PriceWrapper]')->each(function (Crawler $node) {
                            $price        = $node->filter('[class*=styled-components__Price-sc]');
                            $wp['price']  = $this->validate->getText($price);
                            $weight       = $node->filter('[class*=styled-components__UnitLabel]');
                            $wp['weight'] = $this->validate->getText($weight);
                            // print_r($params);
                        });

                    });

            });

        });
    return $params;

}

返回一个空数组($ params)。但是,当我在每个函数中执行print_r()时,它会向我显示值。如何在一个数组中推送每个函数的所有值?任何帮助都会非常值得赞赏。

php foreach each domcrawler
1个回答
0
投票

您必须通过参考函数传递参数。另外,$wp变量在哪里定义?

function getData($data)
{
    $params = [];
    $crawler = new Crawler($data);

    $crawler->filter('#content [class*=ListingCategoryHeader] + ol')
        ->each(function(Crawler $node) use (&$params) { 
            $node->filter('li')
            ->each(function(Crawler $node) use (&$params) {
                $image = $node->filter('img')->attr('src');
                $node->filter('[class*=styled-components__MenuDetailsContainer]')
                ->each(function(Crawler $node) use (&$params) {
                    $product = [];

                    $name = $node->filter('div [class*=styled-components__Name-sc]');
                    $product['name'] = $this->validate->getText($name);

                    $category = $node->filter('div [class*=components__BrandCategory]');
                    $getCategory = $this->validate->getText($category);
                    $modifyCategory = $this->modify->modifyCategoryText($getCategory);
                    $product['category'] = $modifyCategory;

                    $modifyStrain = $this->modify->modifyStrainText($getCategory);
                    $product['strain_type'] = $modifyStrain;
                    $product['prices'] = [];

                    $node->filter('[class*=components__PriceWrapper]')
                    ->each(function(Crawler $node) use (&$product) {
                        $price = $node->filter('[class*=styled-components__Price-sc]');
                        $weight = $node->filter('[class*=styled-components__UnitLabel]');

                        $product['prices'][] = [
                            'price' => $this->validate->getText($price),
                            'weight' => $this->validate->getText($weight);
                        ];
                    });

                    $params[] = $product;
                });
            });
        });

    return $params;
}
© www.soinside.com 2019 - 2024. All rights reserved.