如何根据另一个数组中的值更新数组

时间:2019-02-17 20:55:37

标签: php

我在Undefined index: item_total行上出现错误$totals['item_total'] += (float)$quantity;,我希望$totals数组包含$mycart按产品汇总的数量数据。

$mycart = array();

$mycart[0]['product'] = 'ProductA'; 
$mycart[0]['quantity'] = 10; 

$mycart[1]['product'] = 'ProductA'; 
$mycart[1]['quantity'] = 4; 

$mycart[2]['product'] = 'ProductB'; 
$mycart[2]['quantity'] = 8; 

$mycart[3]['product'] = 'ProductB'; 
$mycart[3]['quantity'] = 8;  

$totals = array();
foreach ($mycart as $row) 
{
  $product = $row['product'];
  $quantity = $row['quantity'];
  $totals['item_total'] += (float)$quantity;
}

3 个答案:

答案 0 :(得分:2)

在php中,您可以通过$totals['item_total'] = value;创建数组索引。如果尚未“初始化”,则不能在表达式中使用数组索引。将此陈述写成$totals['item_total'] += (float)$quantity;“长手”会看到什么?它只是$totals['item_total'] = $totals['item_total'] + (float)$quantity;的快捷方式。在右侧的表达式$totals['item_total']中尚未初始化,因此程序会显示“未定义索引”消息。

答案 1 :(得分:0)

只需在foreach之前添加以下行:

 $totals['item_total']=0;

答案 2 :(得分:0)

您的问题来自使用带有未定义变量的增量

 $totals['item_total'] += (float)$quantity;

这样做的原因是,增量在增量之前会先读取(获取当前值)变量。之所以有意义,是因为我们需要先知道它的当前值,然后才能对其加1。

现在,因为未定义该变量,您将收到一条错误消息Undefined index: item_total"。这也是有道理的,因为我们无法获得(定义)尚未定义的某些事物的值(读取它),因为它尚不存在。

为进一步说明这一点,我们可以手动增加而不使用+=,如下所示:

$totals['item_total'] = $totals['item_total'] + 1;

我们应该同意这与$totals['item_total'] += 1相同,因为它们具有相同的值,但是在这里您可以看到我们必须如何引用该变量的先前值,这与+=是相同的必须做。出于同样的原因,如果未定义,我们将无法读取。

 #Psudo for  $var = $var + 1
  write = read + 1

仅在这样分配时:

  $totals['item_total'] = 0;

不会发生任何读取(未定义值,我们知道什么是0),因此PHP可以使用不存在的变量就可以创建它。有些语言不是那么宽容的。我的意思是说一些语言,您首先需要将$totals定义为数组,然后向其中添加内容。在PHP中,$totals甚至不需要存在$totals['item_total'] = 0;

因此,正如其他人指出的那样,您需要事先将其定义为0的值,这样,当读取完成时,它将知道它为0,并且您不会看到错误。

$totals['item_total'] = 0;
 //or  $totals  = ['item_total' => 0];

foreach ($mycart as $row) 
{
  $product = $row['product'];
  $quantity = $row['quantity'];
  $totals['item_total'] += (float)$quantity;
}

echo $totals['item_total'];

输出

30

Sandbox

PHP实际上对未定义的变量非常宽容,但是在必须首先读取该变量的情况下,必须事先对其进行定义。

更新

基于此评论

  

我希望按产品求和-ProductA = 14,ProductB =16。

您可以这样做:

$mycart = array();

$mycart[0]['product'] = 'ProductA'; 
$mycart[0]['quantity'] = 10; 

$mycart[1]['product'] = 'ProductA'; 
$mycart[1]['quantity'] = 4; 

$mycart[2]['product'] = 'ProductB'; 
$mycart[2]['quantity'] = 8; 

$mycart[3]['product'] = 'ProductB'; 
$mycart[3]['quantity'] = 8;  

$totals = array();

foreach ($mycart as $row) 
{
    $key = $row['product'];

    if(!isset($totals[$key])){
        $totals[$key] = $row;
        // $totals[$key]  = $row['quantity']; //just [key=>quantity] no inner array
    }else{
        $totals[$key]['quantity'] += $row['quantity'];
       // $totals[$key]  += $row['quantity']; //just [key=>quantity] no inner array
    }
}

print_r($totals);

输出

Array
(
    [ProductA] => Array
        (
            [product] => ProductA
            [quantity] => 14
        )

    [ProductB] => Array
        (
            [product] => ProductB
            [quantity] => 16
        )

)

/*
   without the inner array
 Array
(
    [ProductA] => 14
    [ProductB] => 16
 )
*/

Sandbox

请参阅代码中有关如何删除内部数组的注释(如果您只需要Key及其总数量)。如果您确实想要内部数组而不想要顶层键(产品),则可以执行此操作以删除那些内部数组。

//after the loop
$totals = array_values($totals); //resets array keys to numbered keys

这会将ProductAProductB键替换为01

享受。