Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
For better understanding print what the result array should look like. – u_mulder Aug 12 at 7:36
I didn't understand your explanation. Your code uses 6 arrays, and you're only showing 3. And you didn't give an example of the result like u_mulder requested. How do you expect anyone to help? – Pé de Leão Aug 12 at 12:47

1 Answer

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 );
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.