如何使用php重新格式化数组?

阿卜杜斯·萨塔尔·布瓦扬

我有以下数组:

Array
(
    [0] => Array
        (
            [Import] => Array
                (
                    [id] => 1
                    [category_id] => 2
                    [product_id] => 2
                    [amount] => 50
                    [cost] => 8320
                    [comment] => transportation and others cost: 100  
                    [created] => 2015-06-23 19:21:10
                )

            [0] => Array
                (
                    [total_sell] => 10
                    [no_contact] => 1
                    [confirmed] => 2
                    [canceled] => 0
                )

        )

    [1] => Array
        (
            [Import] => Array
                (
                    [id] => 2
                    [category_id] => 2
                    [product_id] => 2
                    [amount] => 15
                    [cost] => 3000
                    [comment] => 
                    [created] => 2015-06-22 18:10:36
                )

            [0] => Array
                (
                    [total_sell] => 10
                    [no_contact] => 1
                    [confirmed] => 2
                    [canceled] => 0
                )

        )

    [2] => Array
        (
            [Import] => Array
                (
                    [id] => 3
                    [category_id] => 2
                    [product_id] => 1
                    [amount] => 15
                    [cost] => 2000
                    [comment] => 
                    [created] => 2015-06-23 19:20:15
                )

            [0] => Array
                (
                    [total_sell] => 10
                    [no_contact] => 0
                    [confirmed] => 0
                    [canceled] => 0
                )

        )

) 

我想删除[Import] [product_id]中相同product_id的重复值,但要加总[Import] [amount]。我期望的数组是:

Array
(
    [0] => Array
        (
            [Import] => Array
                (
                    [id] => 1
                    [category_id] => 2
                    [product_id] => 2
                    [amount] => 65
                    [cost] => 8320
                    [comment] => transportation and others cost: 100  
                    [created] => 2015-06-23 19:21:10
                )

            [0] => Array
                (
                    [total_sell] => 10
                    [no_contact] => 1
                    [confirmed] => 2
                    [canceled] => 0
                )

        )


    [1] => Array
        (
            [Import] => Array
                (
                    [id] => 3
                    [category_id] => 2
                    [product_id] => 1
                    [amount] => 15
                    [cost] => 2000
                    [comment] => 
                    [created] => 2015-06-23 19:20:15
                )

            [0] => Array
                (
                    [total_sell] => 10
                    [no_contact] => 0
                    [confirmed] => 0
                    [canceled] => 0
                )

        )

)

如果有人提供解决此问题的功能,那将是一件真正的礼物。

琥珀色
$filteredArray = [];

foreach ($array as $productData) {
    if (isset($filteredArray[$productData['Import']['product_id']])) {
        $filteredArray[$productData['Import']['product_id']]['Import']['amount'] += $productData['Import']['amount'];
    }
    else {
        $filteredArray[$productData['Import']['product_id']] = $productData;
    }
}

print_r($filteredArray);

啊..忘了提-$array是您的基本阵列。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用 .map 将数组的 php 数组转换为 js 数组以重新格式化

来自分类Dev

使用ajax重新格式化从表单发布的php数组

来自分类Dev

重新格式化php数组

来自分类Dev

如何重新格式化多维数组

来自分类Dev

php中的多维数组重新格式化

来自分类Dev

如何向量化数组重新格式化?

来自分类Dev

如何重新格式化嵌套数据数组?

来自分类Dev

PHP:数组格式化

来自分类Dev

递归重新格式化前缀数组

来自分类Dev

如何格式化数组?

来自分类Dev

如何格式化数组

来自分类Dev

如何使用AWK重新格式化行?

来自分类Dev

如何使用NTFS重新格式化驱动器?

来自分类Dev

如何使用sed重新格式化MAC地址?

来自分类Dev

如何使用emacs脚本重新格式化文件

来自分类Dev

如何使用AWK重新格式化表格数据?

来自分类Dev

如何使用NTFS重新格式化驱动器?

来自分类Dev

如何使用AWK重新格式化行?

来自分类Dev

如何使用PHP json_encode使用[]格式化数组数据

来自分类Dev

PHP重新格式化JSON输出

来自分类Dev

如何使用这种格式格式化PHP日期

来自分类Dev

如何在PHP中格式化数组

来自分类Dev

如何在WordPress / PHP中格式化数组

来自分类Dev

如何在php中格式化数组

来自分类Dev

PHP / Jquery->插件:DataTables,如何在数组中使用格式化程序?

来自分类Dev

PHP / Jquery->插件:DataTables,如何在数组中使用格式化程序?

来自分类Dev

如何重新格式化日期

来自分类Dev

如何重新格式化字典python

来自分类Dev

如何重新格式化显示的日期?