【问题标题】:Array updating temporarily, but not updating permanently in PHP数组临时更新,但不会在 PHP 中永久更新
【发布时间】:2016-10-25 21:19:05
【问题描述】:

代码如下:

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 的 hoursThisPer,它会执行添加到该位置的动作。但是,下次需要更新时,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 

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

【问题讨论】:

  • 添加 & 以更改 foreach 中的数组 foreach($cust_cont as &$cust)
  • 行得通!非常感谢!不过,我会说实话:我不知道为什么会这样。有什么方法可以告诉我它的作用和原因吗?
  • @dmcoding 变量前的& 符号使其成为通过引用传递。如果你不添加这个而不是 $cust 作为副本并且没有引用数组中的值。

标签: php arrays foreach associative-array boolean-logic


【解决方案1】:

您需要通过引用来获取您的数组,否则您只是在更新一个名为 $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_cont 元素值的新变量,而是对该元素的实际引用。

【讨论】:

  • 我选择你的答案主要是因为你是第一个解释答案的人。非常感谢您的帮助!
【解决方案2】:

默认情况下,在 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;
    }
}

我个人觉得很容易错过“&”,所以更喜欢第二个选项,但我确信这甚至不接近普遍观点。

【讨论】:

  • 啊,这解释了为什么我认为可行的方法行不通。通常我确实在您的第二种方法中设置我的数组,但无论出于何种原因,这不是我所做的。非常感谢您的帮助!!
【解决方案3】:

正如 PHP 手册所说:http://php.net/manual/en/control-structures.foreach.php

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 2017-12-02
    • 1970-01-01
    • 2021-10-23
    • 2021-06-13
    • 2023-04-04
    相关资源
    最近更新 更多