Page 1 of 1

Comparing two arrays....

Posted: Mon Sep 22, 2008 5:37 am
by gsairam
Hi All, :P
Please help me out...
Explanation
I have two series of numbers say for example...
1,2,3,4,5,6,7 -- 1st Series...
4,5,6 -- 2nd Series....
Problem Explanation
Now, I need to compare the 2nd series with 1st...
If the number in the 2nd series equals to the number in the 1st series .. I need an output mentioning "1" or else "0"....

Suppose the 1st number in the 2nd series is '4'. Now, I have to compare the 1st series with number '4' and the output must be as follows

0,0,0,1,0,0,0

Like wise it has to continue till the 2nd series is completed.

So the final output must be as follows

0,0,0,1,1,1,0

Means in the first and second series.. numbers 4,5,6 are equal so the output consists of '1' where ever the first and second series are tallied or else '0'.
Please help..Thanks to every one in advance.

Re: Comparing two arrays....

Posted: Mon Sep 22, 2008 6:11 am
by pcoder
I know this is not the better way and it's not optimized, but i think it works:

Code: Select all

 
$firstSeries = array(1,2,3,4,5,6,7);
$secondSeries = array(4,5,6);
$finalSeries = array();
foreach($secondSeries as $secondKey=>$secondVal){
    foreach($firstSeries as $firstKey=>$firstVal){
        if($secondVal == $firstVal){
            $finalSeries[$firstKey]=1;
        }else{
            if($finalSeries[$firstKey] != 1)
                $finalSeries[$firstKey]=0;
        }
    }
}
print_r($finalSeries);
 
 
I love to see any other replies.