Current location: Hot Scripts Forums » Programming Languages » PHP » comparing two arrays help please!


comparing two arrays help please!

Reply
  #1 (permalink)  
Old 09-06-10, 01:49 PM
kentoh kentoh is offline
Newbie Coder
 
Join Date: Jul 2010
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
comparing two arrays help please!

I have a function that I want the closet array to be giving the best "rating" percentage. I have came across this:

Code:
$user = array(1,2,3,4,5);
$us = array(1,2,3,4,5,6);
$us1 = array(1,2,3,4,5);

$count = array_count_values(array_merge($user,$us));
foreach($count as $k => $v) {
 if($v > 1) {
   $hits = $hits + 1;
 
 }
}


echo $hits;

$counter = count($user);
print $counter;
print "<p>percentage: ". $hits / $counter;
I want the $us1 array to be the best matching since it completely matches the $user array.

SO when I put a percentage together they both come back as 100% match. Can someone please help me come up with an idea to make this work. The actual data comes from a mysql database and compares about 10 different arrays to one another and compares them for the best match.

Any and all help is greatly appreciated and thank you in advance.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 09-07-10, 03:13 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Seems like a fair enough challenge. I have a solution that may work for you, but I have a quick question first: how would you like it to treat the following two arrays:
PHP Code:

$arr1 = array( 1234);
$arr2 = array( 12345); 
You'll notice the second has two values of 5. Should they be considered a 100% match?
__________________
The toxic ZCE
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 09-20-10, 10:57 PM
kentoh kentoh is offline
Newbie Coder
 
Join Date: Jul 2010
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
The arrays would never have the same number in them, each number represents a different feature that is a check box.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 09-21-10, 03:33 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Give this a shot and see how it works out for you. I've attempted to comment it the best I can. It returns a float value between 0 and 1, 0 being 0% match, and 1 being 100% match.
PHP Code:

function array_cmp( Array $array_one, Array $array_two )
{

    
// If both are empty, they are identical!
    
if ( empty( $array_one ) && empty( $array_two ) )
    {
        return 
1.;
    }

    
// Ensure unique values only!
    
$array_one array_unique$array_one );
    
$array_two array_unique$array_two );

    
// Count the two array values
    
$count_one count$array_one );
    
$count_two count$array_two );

    
// Calculate their intersection
    
$array_int array_intersect$array_one$array_two );

    
// Count their intersection
    
$count_int count$array_int );

    
// If no intersection, they are nothing alike...
    
if ( === $count_int )
    {
        return 
0.;
    }

    
// ...but if the intersection count matches both, they are identical!
    
elseif ( $count_int === $count_one && $count_int === $count_two )
    {
        return 
1.;
    }

    
// Which is larger? We'd like to subtract the smaller from the larger
    
switch ( TRUE )
    {
        
// array one was larger
        
case ( $count_one $count_two ):
            
$count_cmp  $count_one;
            
$difference = ( $count_one $count_two );
            break;
        
// array two was larger
        
case ( $count_two $count_one ):
            
$count_cmp  $count_two;
            
$difference = ( $count_two $count_one );
            break;
        
// they must then be the same size
        
default:
            
$count_cmp  $count_one;
            
$difference 0;

    }

    
// Divide the difference by the larger count
    // - this will always be a float between `0` and `1`
    // - the further apart the array values, the higher the decimal value
    // - this is backward, so we correct this by subtracting from 1
    // - we then round to the nearest 100th decimal place
    
return round1. floatval$difference $count_cmp ), );


Usage:
PHP Code:

$arr1 = array();
$arr2 = array();
var_dumparray_cmp$arr1$arr2 ) ); // float(1)

$arr1 = array();
$arr2 = array( );
var_dumparray_cmp$arr1$arr2 ) ); // float(0)

$arr1 = array( 1234);
$arr2 = array( 1234);
var_dumparray_cmp$arr1$arr2 ) ); // float(1)

$arr1 = array( 12345678910 );
$arr2 = array( 1234);
var_dumparray_cmp$arr1$arr2 ) ); // float(0.5)

$arr1 = array( 12345678910111213 );
$arr2 = array( 12345678);
var_dumparray_cmp$arr1$arr2 ) ); // float(0.69) 

I hope this works out for you, or is at least a good start.
__________________
The toxic ZCE

Last edited by Keith; 09-21-10 at 03:40 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Arrays and Objects ProBowlUK Visual Basic 2 11-11-08 10:48 AM
arrays, folders and more arrays OnlineDesignz PHP 2 01-01-07 10:08 PM
work on arrays within arrays - i'm stuck... nassau PHP 1 04-29-06 03:37 PM
Arrays with sub arrays perleo PHP 1 09-30-05 08:15 PM
Can record arrays on database? mhs12grade1992 PHP 5 02-17-05 12:20 PM


All times are GMT -5. The time now is 08:50 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.