Как поместить все значения каждой функции в один массив - 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;
}
Который возвращает пустой массив ($params). Однако, когда я делаю print_r() в каждой функции, она показывает мне значения. Как я могу поместить все значения каждой функции в один массив? Любая помощь будет очень ценной.
1 ответ
Решение
Вы должны передать параметры в соответствии с функциями. Кроме того, где находится $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;
}