合并,总结在PHP多个多维数组 [英] Merge and sum multiple multidimensional arrays in PHP

查看:88
本文介绍了合并,总结在PHP多个多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了一个<一个href=\"http://stackoverflow.com/questions/27608404/merge-and-sum-two-multidimensional-arrays-in-php\">question数天前被回答得很好了,但现在我已经实现了解决方案,并面临着几个问题。 (感谢奥列格为得到这个地步)

I asked a question several days ago that was answered well, but now I've implemented the solution and facing a few issues. (Thanks to Oleg for getting this far)

我试图合并,总结如下数组:

I'm trying to merge and sum the following arrays:

Array
(
    [Thursday] => Array
        (
            [Lunch] => Array
                (
                    [Total] => 10
                )

            [Date] => 2015-12-31
        )

)


Array
(
    [Thursday] => Array
        (
            [Lunch] => Array
                (
                    [Guarantees] => 231
                )

            [Date] => 2015-12-31
        )

)


Array
(
    [Friday] => Array
        (
            [Breakfast] => Array
                (
                    [Total] => 1
                )

            [Date] => 2016-01-01
            [Lunch] => Array
                (
                    [Total] => 1
                )

        )

    [Thursday] => Array
        (
            [Lunch] => Array
                (
                    [Total] => 1
                )

            [Date] => 2015-12-31
        )

)

和我使用下面的code:

And I'm using the following code:

private function _merge( $arr1, $arr2, $arr3 )
    {

        $maxArraysCount = func_num_args();
        $return = array();
        for( $i = 1; $i < $maxArraysCount; $i++ ){
            $arr = 'arr' . $i;
            if( isset( $$arr ) && is_array( $$arr )){
                foreach ( $$arr as $day => $value ) {
                    foreach ( $value as $meal => $time ) {
                        if( $meal == "Date"){
                            $return[$day][$meal] = $time;
                        } else {
                            foreach ( $time as $type => $count ) {
                                if( !isset( $return[$day][$meal][$type] ) )
                                    $return[$day][$meal][$type] = 0;
                                $return[$day][$meal][$type] = $count + $return[$day][$meal][$type];
                            }
                        }
                    }
                }
            }
        }
        return $return;
    }

和它的输出是:

Array
(
    [Thursday] => Array
        (
            [Lunch] => Array
                (
                    [Total] => 11
                )

            [Date] => 2015-12-31
        )

    [Friday] => Array
        (
            [Breakfast] => Array
                (
                    [Total] => 1
                )

            [Date] => 2016-01-01
            [Lunch] => Array
                (
                    [Total] => 1
                )

        )

)

有在此合并/总结阵列丢失的信息。请注意有没有三包的说法。看来,ARR1比ARR3更高的优先级?

There is missing information in this merged/summed array. Notice there are no "Guarantees" at all. It appears that arr1 has a higher priority than arr3?

推荐答案

这个问题很简单其实,你需要改变的为:

The problem is very simple actually, you need to change the for to:

for( $i = 1; $i <= $maxArraysCount; $i++ ){

只需添加&LT; =代替&LT;

Just add <= instead of <

这将使for循环作为参数传入,1至3个数组,如果你只是做&LT;它将运行直到$ i小于3,不等于3

This will make the for loop through 1 to 3 arrays passed in as parameters, if you just do < it will run until the $i is less than 3, not equal to 3.

这篇关于合并,总结在PHP多个多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆