我有一个Singleton classe,其属性$culture = []
是一个数组。为了填补它,我创建了一个功能:
public function addCultures($cultures){
array_push($this->culture, $cultures);
}
我正在做一个foreach循环,每次都要添加一个新值。
$culture = new culture();
foreach ($fourchettes as $fourchette) {
$culture->nom = $fourchette['nom'];
$singleton->addCultures($culture);
}
我的问题是,最后,我在这个数组中有3个相同的数据。每次调用要添加的函数时,它都会删除之前的值并将其替换为新值。
第一循环: culture = [" foo"]
第二循环: culture = [" foo2"," foo2"]
第三循环: culture = [" foo3"," foo3"," foo3"]
我想要的是: culture = [" foo"," foo2"," foo3"]
出了什么问题?