在特定数组值上组合数组

时间:2017-10-27 04:49:50

标签: php arrays multidimensional-array

使用数组:

Array
(
    [0] => Array
        (
            [description] => EXAMI
            [pcchrgamt] => 190.00
            [pchrgqty] => 1.00
            [pchrgup] => 190.00
        )

    [1] => Array
        (
            [description] => EXAMI
            [pcchrgamt] => 40.00
            [pchrgqty] => 1.00
            [pchrgup] => 40.00
        )

)

我明白了:

Array
(
    [description] => 0
    [pcchrgamt] => 230
    [pchrgqty] => 2
    [pchrgup] => 230
)

使用:

print_r( $rows1 );
 $sumArray = array();
 foreach ($rows1 as $k=>$subArray) {

 foreach ($subArray as $id=>$value) {
 $sumArray[$id]+=$value;
}
}

print_r($sumArray);

我想得到的是:

Array
(
    [0] => Array
        (
            [description] => EXAMI
            [pcchrgamt] => 230
            [pchrgqty] => 2
            [pchrgup] => 230
        )



)

含义我只想组合数字量。

我也不知道如何处理:

Array
(
    [0] => Array
        (
            [description] => EXAMI
            [pcchrgamt] => 190.00
            [pchrgqty] => 1.00
            [pchrgup] => 190.00
        )

    [1] => Array
        (
            [description] => EXAMI
            [pcchrgamt] => 40.00
            [pchrgqty] => 1.00
            [pchrgup] => 40.00
        )


    [2] => Array
        (
            [description] => EXAMI1
            [pcchrgamt] => 190.00
            [pchrgqty] => 1.00
            [pchrgup] => 190.00
        )

    [3] => Array
        (
            [description] => EXAMI1
            [pcchrgamt] => 40.00
            [pchrgqty] => 1.00
            [pchrgup] => 40.00
        )
)

我希望根据描述和上面的内容进行组合,我希望得到:

Array
    (
        [0] => Array
            (
                [description] => EXAMI
                [pcchrgamt] => 230
                [pchrgqty] => 2
                [pchrgup] => 230
            )
         [1] => Array
            (
                [description] => EXAMI1
                [pcchrgamt] => 230
                [pchrgqty] => 2
                [pchrgup] => 230
            )



    )

描述可能很多,可能有很多,同时可能有一个独特的。

  

如何继续这些

4 个答案:

答案 0 :(得分:3)

这是一种相当天真的方法:

<?php
$records = [
    [
        'desc' => 'EXAMI',
        'amt' => 190.00,
        'qty' => 1,
        'up' => 190.00,
    ],
    [
        'desc' => 'EXAMI',
        'amt' => 40.00,
        'qty' => 1,
        'up' => 40.00,
    ],
    [
        'desc' => 'EXAMI1',
        'amt' => 190.00,
        'qty' => 1,
        'up' => 190.00,
    ],
    [
        'desc' => 'EXAMI1',
        'amt' => 40.00,
        'qty' => 1,
        'up' => 40.00,
    ],
];

function combineArrayOnKey(array $array, $key) {
    $new_array = [];

    foreach($array as $k => $v) {
        if(!is_array($v) || !isset($v[$key]))
            continue;

        // Create elements for new keys
        if(!isset($new_array[$v[$key]])) {
            $new_array[$v[$key]] = $v;
            continue;
        }

        $parent =& $new_array[$v[$key]];

        foreach($v as $kk => $vv) {
            if(!is_numeric($vv))
                continue;

            $parent[$kk] = floatval(@$parent[$kk]) + $vv;
        }
    }

    return array_values($new_array);
}

print_r(combineArrayOnKey($records, 'desc'));

此函数循环遍历记录,并为每个不同的desc键创建单个条目。当遇到具有重复desc的记录时,它会遍历其数字字段并将它们与之前的条目相加。

它从您的示例中提供此输出:

Array
(
    [0] => Array
        (
            [desc] => EXAMI
            [amt] => 230
            [qty] => 2
            [up] => 230
        )

    [1] => Array
        (
            [desc] => EXAMI1
            [amt] => 230
            [qty] => 2
            [up] => 230
        )

)

答案 1 :(得分:0)

试试这个:

$sumArray = array();

 foreach ($rows1 as $k=>$subArray) {

     foreach ($subArray as $key=> $value) {
    if(is_numeric($value)){
                $sumArray[$subArray['description']][$key]+=$value;
         }
         else{
               $sumArray[$subArray['description']][$key]=$value;
         }
     }

}

var_dump($sumArray);

答案 2 :(得分:0)

你可以使用desc作为结果的关键字,你也可以在结果上使用array_values作为关键数字,你可以查看live demo here

$result = [];
foreach($records as $v) {
  $result[$v['desc']]['desc']  = $v['desc'];
  $result[$v['desc']]['amt']  += $v['amt']; 
  $result[$v['desc']]['qty']  += $v['qty']; 
  $result[$v['desc']]['up']   += $v['up']; 
}
print_r($result);

答案 3 :(得分:-1)

您可以尝试使用此代码而不是代码吗

    print_r( $rows1 );
     $sumArray = array();
     foreach ($rows1 as $k=>$subArray) {
        $sumArray[$subArray['description']['description'] = $subArray['description']
        $sumArray[$subArray['description']['amt'] += $subArray['amt']
        $sumArray[$subArray['description']['qty'] += $subArray['qty']
        $sumArray[$subArray['description']['up'] += $subArray['up']

    }

    print_r([$sumArray1, $sumArray2]);

和lemme知道是否有任何问题