Search code examples
phpforeacheachdomcrawler

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


This is my attempt:

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;

}

Which returns an empty array($params). However, when I do print_r() in each function it is showing me the values. How can I push all values of each function in one array?? Any help would be highly appreciable.


Solution

  • You must pass the params by reference to the functions. Also, where is the $wp variable defined?

    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;
    }