0

我想按相同的参考编号对产品求和,但我有第三个参数,例如尺寸(...、24、26mm、...),只有当它们具有相同的尺寸时,我才能对这些值求和。我试过这个:按特定值对多维数组进行分组?但是如何对值求和?我的数组如下所示:

Array
(
    [0] => Array
        (
            [0] => 2
            [1] => 790180X
            [2] => 26mm
        )
    [1] => Array
        (
            [0] => 4
            [1] => 762182Z
        )
    [2] => Array
        (
            [0] => 2
            [1] => 072182X
        )
    [3] => Array
        (
            [0] => 4
            [1] => 660122Y
        )
    [4] => Array
        (
            [0] => 2
            [1] => 790180X
            [2] => 24mm
        )
    [5] => Array
        (
            [0] => 1
            [1] => 225160Y
        )
    [6] => Array
        (
            [0] => 1
            [1] => 244160Y
        )
    [7] => Array
        (
            [0] => 1
            [1] => 225160Y
        )
    [8] => Array
        (
            [0] => 8
            [1] => 954120Y
        )
    [9] => Array
        (
            [0] => 3
            [1] => 072182X
        )
)

我想要这样的事情:

Array
(
    [0] => Array
        (
        [0] => 2
        [1] => 790180X
        [2] => 26mm
        )

    [1] => Array
        (
        [0] => 2
        [1] => 790180X
        [2] => 24mm
        )

    [2] => Array
        (
        [0] => 4
        [1] => 762182Z
        )

    [3] => Array
        (
        [0] => 5
        [1] => 072182X
        )

    [4] => Array
        (
        [0] => 4
        [1] => 660122Y
        )

    [5] => Array
        (
        [0] => 2
        [1] => 225160Y
        )

    [6] => Array
        (
        [0] => 1
        [1] => 244160Y
        )

    [7] => Array
        (
        [0] => 8
        [1] => 954120Y
        )
)

简短版本的数组:

Array               =>  TO =>       Array
(                                   (
    [0] => 2:790180X:26mm           [0] => 2:790180X:26mm
    [1] => 4:762182Z                [1] => 2:790180X:24mm
    [2] => 2:072182X                [2] => 4:762182Z
    [3] => 4:660122Y                [3] => 5:072182X
    [4] => 2:790180X:24mm           [4] => 4:660122Y
    [5] => 1:225160Y                [5] => 2:225160Y
    [6] => 1:244160Y                [6] => 1:244160Y
    [7] => 1:225160Y                [7] => 8:954120Y
    [8] => 8:954120Y                )
    [9] => 3:072182X
)
4

1 回答 1

2

只需将各种“分组”字段用作新数组中的键,将其汇总到一个新数组中:

$sums = array();
foreach ($yourarray as $element) {
    $sums[$element['dimension1']][$element['dimension2']]++;
}

对于您需要“分组”的每个维度,您向 $sums 数组添加一个键。

于 2013-03-08T14:04:19.030 回答