这是我的尝试:
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()时,都会向我显示这些值。如何将每个函数的所有值放入一个数组?任何帮助都是非常可取的。
答案 0 :(得分: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;
}