PHP合并和求和数组

时间:2015-09-13 06:46:08

标签: php arrays

我有一个数组$vd_arr

array (size=3)
  0 => 
    array (size=3)
      'delivnote_id' => string '1' (length=1)
      'qty' => string '76' (length=2)
      'id' => int 17
  1 => 
    array (size=3)
      'delivnote_id' => string '1' (length=1)
      'qty' => string '87' (length=2)
      'id' => int 16
  2 => 
    array (size=3)
      'delivnote_id' => string '1' (length=1)
      'qty' => string '889' (length=3)
      'id' => int 17

我需要通过id和qty值的总和进行合并,结果将是:

array (size=2)
  0 => 
    array (size=3)
      'id' => int 17
      'qty' => int 965
      'delivnote_id' => string '1' (length=1)
  1 => 
    array (size=3)
      'id' => int 16
      'qty' => int 87
      'delivnote_id' => string '1' (length=1)

这个脚本完成了这项工作,但是php注意到了我undefined index: qty

$vendel = array();
$indexArray = array();
foreach ($vd_arr as $miniarray) {
  $vendel[$miniarray['id']]['id'] = $miniarray['id'];
  $vendel[$miniarray['id']]['qty'] += $miniarray['qty'];
  $vendel[$miniarray['id']]['delivnote_id'] = $miniarray['delivnote_id'];
}

foreach($vendel as $value) {
  $indexArray[] = $value;
}

return $indexArray;

如何从上面的脚本中避免undefined index?感谢。

5 个答案:

答案 0 :(得分:2)

试试这个

$vendel = array();
$indexArray = array();
foreach ($vd_arr as $miniarray) {
  $vendel[$miniarray['id']]['id'] = $miniarray['id'];

  if(isset($vendel[$miniarray['id']]['qty']))
  {
      $vendel[$miniarray['id']]['qty'] += $miniarray['qty'];
  }
  else 
  {
      $vendel[$miniarray['id']]['qty'] = $miniarray['qty'];
  } 

  $vendel[$miniarray['id']]['delivnote_id'] = $miniarray['delivnote_id'];
}

foreach($vendel as $value) {
  $indexArray[] = $value;
}

return $indexArray;

答案 1 :(得分:0)

在访问之前,修改代码包括检查数组属性:

    $vendel = array();
    $indexArray = array();
    foreach ($vd_arr as $miniarray) {

        // Skips adding this mini array if an ID is note present
        if(($id = isset($miniarray['id']) ? $miniarray['id'] : null) === null) {
            continue;
        }

        // The default values at the end of these lines can be swapped out for more suitable defaults
        $qty = isset($miniarray['qty']) ? $miniarray['qty'] : 0;
        $delivnote_id = isset($miniarray['delivnote_id']) ? $miniarray['delivnote_id'] : null;

        $vendel[$id]['id'] = $id;
        $vendel[$id]['qty'] += $qty;
        $vendel[$id]['delivnote_id'] = $delivnote_id;
    }

    foreach($vendel as $value) {
        $indexArray[] = $value;
    }

    return $indexArray;

这也会跳过添加没有'id'的$ miniarray项目。

答案 2 :(得分:0)

  • 根据提供的代码,问题出现在$ vendel [$ miniarray ['id']] ['qty'] + = $ miniarray ['qty'];
  • 由于您尚未定义$ vendel [$ miniarray ['id']] ['qty']的值,因此弹出此问题。
  • 要解决此问题,您需要为$ vendel [$ miniarray ['id']] ['qty']定义初始值。
  • 您需要在您编写的foreach语句之前将qty的初始值定义为零,如下所示。
  • foreach($ vd_arr as $ miniarray){$ vendel [$ miniarray ['id']] ['qty'] = 0;}

答案 3 :(得分:0)

如前所述,只需在添加之前检查是否设置了数量:

$vendel = array();
$indexArray = array();
foreach ($vd_arr as $miniarray) {

  $vendel[$miniarray['id']]['id'] = $miniarray['id'];
  $vendel[$miniarray['id']]['delivnote_id'] = $miniarray['delivnote_id'];
  // the magic is in the next line
  if(!isset($vendel[$miniarray['id']]['qty']))
      $vendel[$miniarray['id']]['qty'] = 0;
  $vendel[$miniarray['id']]['qty'] += $miniarray['qty'];
}

foreach($vendel as $value) {
  $indexArray[] = $value;
}
return $indexArray;
通过一些重构我们可以摆脱第二个for循环,如果我们使用' id'作为$ indexArray的关键

$vendel = array();
$indexArray = array();

foreach ($vd_arr as $miniarray) {
    if(!isset($indexArray[$miniarray['id']])) {
        $indexArray[$miniarray['id']] = array(
            'delivnote_id' => $miniarray['delivnote_id']
            ,'qty' => 0
        );
    }
    $indexArray[$miniarray['id']]['qty'] += $miniarray['qty'];
}

return $indexArray;

或者,如果您需要这些索引是数字

$vendel = array();
$indexArray = array();
foreach ($vd_arr as $miniarray) {
    if(!isset($indexArray[$miniarray['id']])) {
        $indexArray[$miniarray['id']] = array(
            'id' => $miniarray['id']
            ,'delivnote_id' => $miniarray['delivnote_id']
            ,'qty' => 0
        );
    }
    $indexArray[$miniarray['id']]['qty'] += $miniarray['qty'];
}
return array_values($indexArray);

答案 4 :(得分:0)

另一种使用功能编程的方法

app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // get information from html forms