Jump to content

noob needs help......


loudog

Recommended Posts

hey guy's, Im trying to compare two array's  to see it they have any common variables(i think that's what they are called) if they do have the function print out the common variable if they don't have the function print something saying they don't. Anyway, Im having trouble  comparing the actual  arrays  this is what i have so far.                 

                                                                                                                                                              thanks in advance.

<?php
function dz_compare($a,$b){
     $v=$a[$key];
     $v1=$b
     
     foreach($a as $key=>$value)
        foreach($b as $key =>$value)
         if($v1)
         
         
          
            
        
      
     }
}
$a=array(1=>"dog",2=>"cat",3=>"horse");
$b=array(1=>"horse",2=>"mouse",3=>"fish");

?>

Link to comment
https://forums.phpfreaks.com/topic/245175-noob-needs-help/
Share on other sites

There is a function in php called array_diff().  It will compare two arrays and output the difference.

 

You can always search the manual here..

http://php.net/manual/en/function.array-diff.php

 

Here is a good example..

$a=array(1=>"dog",2=>"cat",3=>"horse");
$b=array(1=>"horse",2=>"mouse",3=>"fish");
$result = array_diff($a, $b);
print_r($result);

 

 

You can also use the intersect function to see which items appear in both arrays like this..

$a=array(1=>"dog",2=>"cat",3=>"horse");
$b=array(1=>"horse",2=>"mouse",3=>"fish");
$result = array_intersect($a, $b);
print_r($result);

Link to comment
https://forums.phpfreaks.com/topic/245175-noob-needs-help/#findComment-1259299
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.