dongrunying7537 2018-03-09 16:54
浏览 30
已采纳

使用PHP合并数组中的数据

Hello I have this array:

Array
(
    [2018/03/01] => Array
        (
            [0] => Array
                (
                    [BIL_Date] => 2018/03/01
                    [BIL_RateNonTaxed] => 105.00
                    [BIL_RateTaxed] => 115.500000000
                    [BIL_Status] => notcharged
                )

            [1] => Array
                (
                    [BIL_Date] => 2018/03/01
                    [BIL_RateNonTaxed] => 60.00
                    [BIL_RateTaxed] => 63.000000000
                    [BIL_Status] => charged
                )

            [2] => Array
                (
                    [BIL_Date] => 2018/03/01
                    [BIL_RateNonTaxed] => 21.00
                    [BIL_RateTaxed] => 25.194750000
                    [BIL_Status] => notcharged
                ) 
        )
)

How can I merge some datas to have something like this ?

I search to group by date, item charged and notcharged.

Array
(
    [2018/03/01] => Array
        (
            [notcharged] => Array
                (
                    [BIL_RateNonTaxed] => 126.00
                    [BIL_RateTaxed] => 140.69475
                )
            [charged] => Array
                (
                    [BIL_RateNonTaxed] => 60.00
                    [BIL_RateTaxed] => 63.000000000
                )
        )
)

Here what I tried:

$datas = array();
foreach ($array as $key => $element) {
    $datas[$key][] = "BIL_RateNonTaxed" => $key['BIL_RateNonTaxed'];
    $datas[$key][] = "BIL_RateTaxed" => $key['BIL_RateTaxed'];
}

Could you please help me with this please ?

Thanks.

  • 写回答

2条回答 默认 最新

  • doqpm82240 2018-03-09 17:12
    关注

    You can loop thru the array and use array_reduce to summarize the data.

    $array = array
    (
        "2018/03/01" => array
            (
               array
                    (
                        "BIL_Date" => '2018/03/01',
                        "BIL_RateNonTaxed" => '105.00',
                        "BIL_RateTaxed" => '115.500000000',
                        "BIL_Status" => 'notcharged'
                    ),
    
                array
                    (
                        "BIL_Date" => '2018/03/01',
                        "BIL_RateNonTaxed" => '60.00',
                        "BIL_RateTaxed" => '63.000000000',
                        "BIL_Status" => 'charged'
                    ),
    
               array
                    (
                        "BIL_Date" => '2018/03/01',
                        "BIL_RateNonTaxed" => '21.00',
                        "BIL_RateTaxed" => '25.194750000',
                        "BIL_Status" => 'notcharged'
                    ) 
            ),
    );
    
    $result = array();
    foreach ($array as $key => $element) {
        $result[ $key ] = array_reduce( $element, function ($c, $v) {
            if( !isset( $c[ $v["BIL_Status"] ] ) ) {
                $c[ $v["BIL_Status"] ] = array(
                    "BIL_RateNonTaxed" => $v["BIL_RateNonTaxed"],
                    "BIL_RateTaxed" => $v["BIL_RateTaxed"],
                );
            } else {
                $c[ $v["BIL_Status"] ] = array(
                    "BIL_RateNonTaxed" => ( $c[ $v["BIL_Status"] ]["BIL_RateNonTaxed"] + $v["BIL_RateNonTaxed"] ),
                    "BIL_RateTaxed" => ( $c[ $v["BIL_Status"] ]["BIL_RateTaxed"] + $v["BIL_RateTaxed"] ),
                );
            }
            return $c;
        }, array() );
    }
    
    echo "<pre>";
    print_r( $result );
    echo "</pre>";
    

    This will result to:

    Array
    (
        [2018/03/01] => Array
            (
                [notcharged] => Array
                    (
                        [BIL_RateNonTaxed] => 126
                        [BIL_RateTaxed] => 140.69475
                    )
    
                [charged] => Array
                    (
                        [BIL_RateNonTaxed] => 60.00
                        [BIL_RateTaxed] => 63.000000000
                    )
    
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计