在PHP中暂时更新数组,但不永久更新

在PHP中暂时更新数组,但不永久更新,php,arrays,foreach,associative-array,boolean-logic,Php,Arrays,Foreach,Associative Array,Boolean Logic,代码如下: if($condition == 'condition1' || $condition == 'condition2') { $found = false; //loop through the array of customers contracts foreach($cust_cont as $cust) { //if the customer is found if ($cust["customer"]

代码如下:

if($condition == 'condition1' || $condition == 'condition2')
{   
    $found = false;
    //loop through the array of customers contracts
    foreach($cust_cont as $cust)
    {   
        //if the customer is found
        if ($cust["customer"] == $customer) 
        {
            $temp = floatval($cust["hoursThisPer"]);
            $temp += $time;
            $cust["hoursThisPer"] = $temp;
            $found = true;
        }
    }
    if ($found == false)
    {
        $cust_cont[] = array("customer" => "$customer", "hoursUsed" => $hoursUsed, 
           "hoursAvail" => $allowed, "hoursThisPer" => (0 + $time));
    }
}
所以,我要做的是遍历一个数组。如果数组中没有客户条目,我想将时间添加到该客户的已用时间中。如果客户的条目不存在,我想在数组中为该客户创建一个条目并初始化其值

数组的条目已正确初始化,但当我尝试更新它们时,出现了一些奇怪的情况。例如,如果我在数组中有customer1,并且我想将其添加到customer1的hoursthiper中,那么它将经历添加到该点的动作。但是,下次需要更新时,customer1的hoursThisPer将设置为初始值,而不是更新后的值。我无法找出我逻辑上的缺陷。非常感谢您的帮助。我有一些样本输出

Customer1:0.25

time: 0.25

temp: 0.5

0.5

Customer1:0.25

time: 1.50

temp: 1.75

1.75

Customer1:0.25

time: 0.50

temp: 0.75

0.75 

格式为客户:初始时间;时间补充;初始时间+增加时间的预期总时间;“更新”后的数组值;找到客户的下一个实例(循环继续)。

您需要通过引用获取数组,否则您只是更新一个名为
$cust
的新变量:

if($condition == 'condition1' || $condition == 'condition2')
{   
    $found = false;
    //loop through the array of customers contracts
    foreach($cust_cont as &$cust)
    {   
        //if the customer is found
        if ($cust["customer"] == $customer) 
        {
            $temp = floatval($cust["hoursThisPer"]);
            $temp += $time;
            $cust["hoursThisPer"] = $temp;
            $found = true;
        }
    }
    if ($found == false)
    {
        $cust_cont[] = array("customer" => "$customer", "hoursUsed" => $hoursUsed, 
           "hoursAvail" => $allowed, "hoursThisPer" => (0 + $time));
    }
}

在这里,我在
foreach
循环中的
$cust
声明之前添加了
&
。使用此
$cust
不是具有当前
$cust\u cont
元素值的新变量,而是对该元素的实际引用。

默认情况下,在foreach循环中创建的变量($cust在本例中)是通过值而不是引用创建的。 您可以将其更改为“通过引用传递”(如splash58在注释中所建议的那样,通过前缀为&),允许您通过更改创建的变量来更改原始数组:

foreach($cust_cont as &$cust)
{   
    //if the customer is found
    if ($cust["customer"] == $customer) 
    {
        $temp = floatval($cust["hoursThisPer"]);
        $temp += $time;
        $cust["hoursThisPer"] = $temp;
        $found = true;
    }
}
也可以直接获取相关索引并编辑数组

foreach($cust_cont as $index => $cust)
{   
    //if the customer is found
    if ($cust["customer"] == $customer) 
    {
        $temp = floatval($cust["hoursThisPer"]);
        $temp += $time;
        $cust_cont[$index]["hoursThisPer"] = $temp;
        $found = true;
    }
}
就我个人而言,我发现很容易错过“&”,所以我更喜欢第二个选项,但我确信这还不是一个普遍的观点。

正如PHP手册所说:

为了能够直接修改循环中的数组元素,请在$value之前加上&。在这种情况下,将通过引用指定值


添加并更改foreach
foreach($cust\u cont as&$cust)
中的数组,这样做有效!非常感谢你!不过,老实说,我不知道为什么会这样。有什么方法可以告诉我它的作用和为什么起作用吗?@dmcoding在变量成为passby-by引用之前对
&
符号进行编码。如果你不把它作为拷贝添加到
$cust
中,并且没有对数组中的值的引用。啊,这解释了为什么我认为有效的东西不起作用。通常我会在第二种方法中设置数组,但不管出于什么原因,我都不会这样做。非常感谢你的帮助!!我选择你的答案主要是因为你是第一个解释答案的人。我真的很感谢你的帮助!