使用php将多个数组与键组合成其他数组

时间:2013-08-12 07:04:18

标签: php arrays foreach

我们有三个这样的数组,但这些都是动态的

Array
(
    [04/07/2013] => 2
    [05/02/2013] => 1
    [06/02/2013] => 1
    [08/07/2013] => 2
    [08/08/2013] => 3
    [09/07/2013] => 2
    [11/07/2013] => 1
    [16/03/2013] => 1
    [17/07/2013] => 1
    [18/04/2013] => 2
    [18/07/2013] => 1
    [21/05/2013] => 2
    [24/05/2013] => 8
    [25/04/2013] => 2
    [26/04/2013] => 1
    [26/06/2013] => 1
    [30/05/2013] => 1
)
Array
(
    [01/08/2013] => 42
    [02/08/2013] => 2
    [03/08/2013] => 3
    [07/08/2013] => 29
    [09/08/2013] => 4
    [10/08/2013] => 4
    [11/08/2013] => 31
    [19/07/2013] => 4
    [20/07/2013] => 4
    [22/07/2013] => 13
    [23/07/2013] => 69
    [29/07/2013] => 4
    [31/07/2013] => 5
)
Array
(
    [13/02/2013] => 2
    [26/04/2013] => 2
    [04/06/2013] => 2
    [20/06/2013] => 2
    [04/07/2013] => 2
    [09/07/2013] => 1
    [01/08/2013] => 1
    [07/08/2013] => 1
    [08/08/2013] => 3
)

我们希望用键组合到其他数组中(键必须保持相同)。如果所有三个数组键都相同,则将其放入同一个键中另外明智地创建一个键,如果数组有其他明智的键,则将其设置为值它没有价值。

我们在这里尝试了这个

$maximum = max($countVisi,$countClic,$countClai);
    if($countClic==$maximum){
        $maxim = $clickArray;
    }elseif($countVisi>=$maximum){
        $maxim = $visitArray;
    }elseif($countClai>=$maximum){
        $maxim = $claimsArray;
    }else{
        $maxim = $visitArray;
    }

我们计算最大索引数组并像这样预先循环

foreach($maxim as $key=>$values){
        if($visitArray[$key]){
            $vv[$key] = $visitArray[$key];
        }else{
            $vv[$key] = 0;
        }
        if($clickArray[$key]){
            $cc[$key] = $clickArray[$key];
        }else{
            $cc[$key] = 0;
        }
        if($claimsArray[$key]){
            $kk[$key] = $claimsArray[$key];
        }else{
            $kk[$key] = 0;
        }
        $combineArrayNext[$key][] = $vv[$key];
        $combineArrayNext[$key][] = $cc[$key];
        $combineArrayNext[$key][] = $kk[$key];
        //$vvvvv = explode('/' , $key);
        //$myKey[$key] = "'".date('d M Y' , strtotime($vvvvv[2]."/".$vvvvv[1]."/".$vvvvv[0]))."'";
    }

问题是我们只是按照最大数组键获得。它保留了最大数组中不存在的键。

对不起解释,我想你理解我的问题。请与我们分享一些想法来解决我的问题。

由于

1 个答案:

答案 0 :(得分:0)

希望此代码可以帮助您:

$dates = array_merge(array_keys($arr1), array_keys($arr2), array_keys($arr3));
foreach($dates as $date) {
  $new_arr[$date] = array('click' => isset($arr1[$date])? $arr1[$date] : 0,
                          'visit' => isset($arr2[$date])? $arr2[$date] : 0,
                          'claim' => isset($arr3[$date])? $arr3[$date] : 0 );
}