1 回答

TA贡献2021条经验 获得超8个赞
尝试在类的最开始创建一个属性,例如
private $exportedProductsIds = [];
然后在每次迭代之后,在返回之前将导出的产品 ID 放入其中
array_push($this->exportedProductsIds, $objProduct->id);
然后在运行前添加验证,如果 id 已经在$exportedProductsIds然后跳过迭代
if (!in_array($objProduct->id, $this->exportedProductsIds)) {
//run your code
}
但我认为放置此代码的最佳位置是您调用getProductFromArray方法的地方。
我假设是这样的
private function exportProducts($products)
{
foreach ($products as $product) {
if (in_array($objProduct->id, $this->exportedProductsIds)) {
continue;
}
if ($this->getProductFromArray($arrProduct)) {
array_push($this->exportedProductsIds, $objProduct->id);
}
}
}
- 1 回答
- 0 关注
- 111 浏览
添加回答
举报