dryl34156 2013-08-12 07:04
浏览 49

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

We have three array something like this but these are dynamic

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
)

We want to combine into other array with keys(key must be remain same).If all three array keys has same put it into the same key other wise create a key for and put it value if array has key other wise put it with zero value.

Here we tried for this

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

we count the maximum index array and foreach the loop like this

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]))."'";
    }

The problem is that we are getting only according to max array key.It leave the key which are not exits in max array.

Sorry for the less explaining , I think you understand my problem.Please share some idea with us to solve my problem.

Thanks

  • 写回答

1条回答 默认 最新

  • duanhuan5409 2013-08-12 08:24
    关注

    Hope this code helps you:

    $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 );
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程