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

how to push all the values of each function in one array - PHP DomCrawler

这是我的尝试:

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;

}

其中 returns 一个空数组 ($params)。但是,当我在每个函数中执行 print_r() 时,它会向我显示值。如何将每个函数的所有值推送到一个数组中?任何帮助将不胜感激。

您必须通过对函数的引用来传递参数。还有,$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;
}