Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php msyterious值更改-通过引用调用?_Php - Fatal编程技术网

Php msyterious值更改-通过引用调用?

Php msyterious值更改-通过引用调用?,php,Php,我正在做一个项目,它基本上把一些数据从一个表映射到另一个表。 我反复浏览产品列表,找出我有哪些语言的翻译 我有以下代码: foreach ($allLanguages AS $languageID => $language) { foreach ($allProducts AS $singleProduct) { if (in_array ($languageID, $productLanguages[$singleProduct->id]))

我正在做一个项目,它基本上把一些数据从一个表映射到另一个表。 我反复浏览产品列表,找出我有哪些语言的翻译

我有以下代码:

foreach ($allLanguages AS $languageID => $language) {
        foreach ($allProducts AS $singleProduct) {
            if (in_array ($languageID, $productLanguages[$singleProduct->id])) {
                $singleProduct->lang_id = $productId;
                $singleProduct->language = $language['language'];
                $singleProduct->country = $language['country'];
                print $singleProduct->id . " - " .$language['language']."_".$language['country']."\n";
                $languageProducts[] = $singleProduct;

                $productId ++;
            }
        }
    }

    print "after loops: \n";
    foreach ($languageProducts AS $product) {
        print $product->id . " - " .$product->language."_".$product->country."\n";
    }
它产生如下输出:

// put any code in {}
1 - de_DE  
2 - de_DE  
3 - de_DE  
1 - de_AT  
2 - de_AT  
循环后:

1 - de_AT  
2 - de_AT  
3 - de_DE  
1 - de_AT  
2 - de_AT  
前五行(内部输出)是正确的(根据我的数据)和预期的。 但随后,值“神秘地”从循环内的正确值变为循环外的错误值。
看起来它们被覆盖了,但总行数仍然正确。只有de_de中存在的一条记录不变

所以我只能猜测:这是一种引用调用问题吗? 有人能给我指出正确的方向吗? 非常感谢

如果您将
echo“处理对象:”;var_dump(单一产品)
就在内部
foreach
循环中,您将看到您正在为每种语言处理相同的对象实例ID

您需要克隆该对象以获取其副本

foreach($allProducts as $singleProduct) {
    $singleProduct = clone $singleProduct;
    // ...
}

这将为您提供不会相互覆盖的对象副本。

非常感谢。这很有效。虽然我不确定我是否完全理解这里发生了什么