php - Aide à la réorganisation des tableaux ~~
伊谢尔伦
伊谢尔伦 2017-06-06 09:54:16
0
3
689

Les tableaux suivants ont le même gid et nécessitent que Num soit accumulé. Ceux avec des gid différents sont affichés directement sans aucune opération. Comment réaliser la déduplication de tableau ?

Array
(
    [0] => Array
        (
            [id] => 1
            [no] => WF20170001
            [wid] => 1
            [gid] => 1
            [num] => 4
            [price] => 2.55
            [amount] => 10.20
            [buyer] => 顾文君
            [invoice] => 123456789
            [date] => 2017-03-28
            [remarks] => 测试数据
            [cid] => pc001
            [class] => 办公用品
            [name] => 笔记本电脑
            [type] => Acer Aspire E1-410G
            [unit] => 台
            [warehouse_name] => 办公用品仓库
        )

    [1] => Array
        (
            [id] => 3
            [no] => WF20170001
            [wid] => 1
            [gid] => 5
            [num] => 5
            [price] => 100.00
            [amount] => 500.00
            [buyer] => 顾文君
            [invoice] => 1231241
            [date] => 2017-04-14
            [remarks] => 入库测试
            [cid] => tf001
            [class] => 办公用品
            [name] => 打印机碳粉
            [type] => 88A
            [unit] => 支
            [warehouse_name] => 办公用品仓库
        )

    [2] => Array
        (
            [id] => 4
            [no] => WF20170001
            [wid] => 1
            [gid] => 2
            [num] => 1
            [price] => 4500.00
            [amount] => 4500.00
            [buyer] => 张秀珍
            [invoice] => 4558895
            [date] => 2017-04-09
            [remarks] => 入库测试
            [cid] => pc002
            [class] => 办公用品
            [name] => 笔记本电脑
            [type] => Acer Aspire E5-571G-58WT
            [unit] => 台
            [warehouse_name] => 办公用品仓库
        )

    [3] => Array
        (
            [id] => 5
            [no] => WF20170002
            [wid] => 1
            [gid] => 3
            [num] => 1
            [price] => 88.00
            [amount] => 88.00
            [buyer] => 顾文君
            [invoice] => 556789
            [date] => 2017-04-09
            [remarks] => 测试数据
            [cid] => ow
            [class] => 办公用品
            [name] => 万用表
            [type] => 万用表
            [unit] => 台
            [warehouse_name] => 办公用品仓库
        )

    [4] => Array
        (
            [id] => 12
            [no] => WF20170001
            [wid] => 1
            [gid] => 1
            [num] => 1
            [price] => 2.55
            [amount] => 2.55
            [buyer] => 顾文君
            [invoice] => 53412312
            [date] => 2017-06-05
            [remarks] => 无
            [cid] => pc001
            [class] => 办公用品
            [name] => 笔记本电脑
            [type] => Acer Aspire E1-410G
            [unit] => 台
            [warehouse_name] => 办公用品仓库
        )

)
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

répondre à tous(3)
刘奇
     $arr = array(
                array(
                        'gid' => 1,
                        'num' => 4,
                    ),
                array(
                        'gid' => 1,
                        'num' => 4,
                    ),
                array(
                        'gid' => 3,
                        'num' => 3,
                    ),
                array(
                        'gid' => 4,
                        'num' => 4,
                    ),
         );
    
    $result = array();
    foreach($arr as $val) {
        if(!isset($result[$val['gid']])) {
            $result[$val['gid']] = $val;
            continue;
        }
        
        $result[$val['gid']]['num'] += $val['num'];
    }
    
    var_dump("<pre>", $result);die;
滿天的星座

Avez-vous découvert cela grâce aux données de la base de données ? Si tel est le cas, utilisez directement l'instruction SQL pour dédupliquer le gid, puis utilisez sum() pour accumuler le num, et vous pourrez obtenir le résultat.

習慣沉默
$ret = [['gid' => 2,'num' => 5],['gid' => 3,'num' => 5],['gid' => 2,'num' => 6]];
$newret = array();
foreach($ret as $key => $val){
    $newret[$val['gid']]['gid'] = $val['gid'];
    $newret[$val['gid']]['num'] += $val['num'];
}
sort($newret);


//结果
Array
(
    [0] => Array
        (
            [gid] => 2
            [num] => 11
        )
    [1] => Array
        (
            [gid] => 3
            [num] => 5
        )
)
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal