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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.