移位数组成员向上一级和匹配键的SUM值

时间:2016-01-31 09:20:56

标签: php arrays foreach associative-array

我有这个数据数组:

array (size=3)
  123 => 
    array (size=1)
      1 => string '50000.00' (length=8)
  126 => 
    array (size=1)
      3 => string '20000.00' (length=8)
  86 => 
    array (size=1)
      3 => string '6000.00' (length=7)

我正在尝试将每个子阵列向上移动一级,如果键匹配则尝试将总数相加。例如,结果应如下所示:

array (size=2)
  1 => string '50000.00' (length=8)
  3 => string '26000.00' (length=8)

我试过了:

foreach ($weight_arr as $value)
 {
   $new_arr += $value;
 }

但结束时:

array (size=2)
  1 => string '50000.00' (length=8)
  3 => string '20000.00' (length=8)

我尝试过其他一些解决方案,但都失败了。我必须遗漏一些东西,这似乎是一件很简单的事情。

编辑:

原始数据:

array (size=7)
  0 => 
    array (size=15)
      'id' => int 17
      'wo_id' => int 12
      'prod_id' => int 123
      'qty' => int 3
      'cat_id' => int 12
      'blend' => null
      'length' => null
      'lbs' => string '50000.00' (length=8)
      'delay' => null
      'unit' => null
      'created_at' => string '2015-06-12 14:15:10' (length=19)
      'updated_at' => string '2015-06-12 14:15:10' (length=19)
      'deleted_at' => null
      'product_id' => int 123
      'truck_types_id' => int 2
  1 => 
    array (size=15)
      'id' => int 18
      'wo_id' => int 12
      'prod_id' => int 123
      'qty' => int 3
      'cat_id' => int 12
      'blend' => null
      'length' => null
      'lbs' => string '50000.00' (length=8)
      'delay' => null
      'unit' => null
      'created_at' => string '2015-06-12 14:15:10' (length=19)
      'updated_at' => string '2015-06-12 14:15:10' (length=19)
      'deleted_at' => null
      'product_id' => int 123
      'truck_types_id' => int 1
  2 => 
    array (size=15)
      'id' => int 19
      'wo_id' => int 12
      'prod_id' => int 126
      'qty' => int 2
      'cat_id' => int 12
      'blend' => null
      'length' => null
      'lbs' => string '20000.00' (length=8)
      'delay' => null
      'unit' => null
      'created_at' => string '2015-06-14 23:28:25' (length=19)
      'updated_at' => string '2015-06-14 23:28:25' (length=19)
      'deleted_at' => null
      'product_id' => int 126
      'truck_types_id' => int 2
  3 => 
    array (size=15)
      'id' => int 20
      'wo_id' => int 12
      'prod_id' => int 126
      'qty' => int 2
      'cat_id' => int 12
      'blend' => null
      'length' => null
      'lbs' => string '20000.00' (length=8)
      'delay' => null
      'unit' => null
      'created_at' => string '2015-06-14 23:28:25' (length=19)
      'updated_at' => string '2015-06-14 23:28:25' (length=19)
      'deleted_at' => null
      'product_id' => int 126
      'truck_types_id' => int 3
  4 => 
    array (size=15)
      'id' => int 12
      'wo_id' => int 12
      'prod_id' => int 86
      'qty' => int 25
      'cat_id' => int 11
      'blend' => null
      'length' => null
      'lbs' => string '6000.00' (length=7)
      'delay' => null
      'unit' => null
      'created_at' => string '2016-01-21 00:33:59' (length=19)
      'updated_at' => string '2016-01-21 00:33:59' (length=19)
      'deleted_at' => null
      'product_id' => int 86
      'truck_types_id' => int 3
  5 => 
    array (size=15)
      'id' => null
      'wo_id' => int 12
      'prod_id' => int 88
      'qty' => int 15
      'cat_id' => int 11
      'blend' => null
      'length' => null
      'lbs' => null
      'delay' => null
      'unit' => null
      'created_at' => string '2016-01-21 00:33:59' (length=19)
      'updated_at' => string '2016-01-21 00:33:59' (length=19)
      'deleted_at' => null
      'product_id' => null
      'truck_types_id' => null
  6 => 
    array (size=15)
      'id' => null
      'wo_id' => int 12
      'prod_id' => int 90
      'qty' => int 10
      'cat_id' => int 11
      'blend' => null
      'length' => null
      'lbs' => null
      'delay' => null
      'unit' => null
      'created_at' => string '2016-01-21 00:33:59' (length=19)
      'updated_at' => string '2016-01-21 00:33:59' (length=19)
      'deleted_at' => null
      'product_id' => null
      'truck_types_id' => null

并且使用以下方法创建数组:

foreach($prodArr as $prod)
 {
  if(!is_null($prod['lbs']))
    if(empty($weight_arr[$prod['truck_types_id']])) $weight_arr[$prod['prod_id']] = array($prod['truck_types_id'] => $prod['lbs'] );
 }   

2 个答案:

答案 0 :(得分:1)

由于你还没有发布整个代码,我刚从头开始重写,因为我理解你的任务。请评论,如果我误解了那里......

<?php

$a = [
    123 => [1 => '50000.00'],
    126 => [3 => '20000.00'],
    86 => [3 => '6000.00']
    ];
var_export($a); 

$na = [];

foreach ($a as $subArray) {
    foreach ($subArray as $key => $value) {
        if (array_key_exists($key, $na)) $na[$key] += $value;
        else $na[$key] = $value;
    }
}

var_export($na);

请注意,使用floatval来处理浮点数而不是字符串可能是个好主意,尽管PHP可以很好地处理它,上面的代码不使用浮点数就可以了。

答案 1 :(得分:0)

$arr = [
  123 => 
    [ 1 => '50000.00' ],
  126 => 
    [ 3 => '20000.00' ],
  86 => 
    [ 3 => '6000.00'  ]
];
$decimals = '.';
$thousands = '';

$new = array();

foreach($arr as $firstlevel) {
    foreach($firstlevel as $key => $val) {

        /* Since we have strings representing floats we need to 
         * transform them into float first and transform them
         * back to their string after adding them together
         */
        if(isset($new[$key])) {
            $new[$key] = number_format( 
                floatval($new[$key]) + floatval($val), 
                2, 
                $decimals, 
                $thousands
            );

        } else {
            $new[$key] = $val;
        }
    }
}

echo '<pre>';
print_r($new);
echo '</pre>';

如果您需要完全符合该字符串格式,请阅读php文档中的number_format()floatval()。要解决您的问题,您需要使用foreach循环。第一个遍历数组的每个元素,第二个遍历每个子数组的每个元素。