Click to See Complete Forum and Search --> : [RESOLVED] compare array


yunfanny
07-11-2006, 11:07 PM
I am writing the code to compare the strings from two array : $masterlistfreq
and $docstemmedwordsfreq, where
$masterlistfreq is ARRAY Array ( [about ] => 1 [book ] => 1
[car ] =>1 ......[work ] => 1 [worker ] => 1 [zone] => 1.......)
$docstemmedwordsfreq is ARRAY Array ( [biggest] => 2 [company ] => 1 [work ] => 1 )



<?php
foreach ($masterlistfreq as $wordkeys => $wordvalues)
{ foreach ($docstemmedwordsfreq as $keys => $values)
{ print_r ($wordkeys);
print_r ($keys);
$compare = strcasecmp("$wordkeys","$keys");
print_r ($compare);
}
}
?>


The expected result when comparing $wordkeys=="work" and $keys=="work", should be work work 0. But the result given is work work 13

What is wrong here?

Thank You

sitehatchery
07-12-2006, 01:16 AM
Here's what it looks like:

print_r ($wordkeys);
print_r ($keys);

$wordkeys has a value of 1, and $keys has a value of 3. Since you don't have a break, "13" is printed to the screen.

Try removing:
print_r ($wordkeys);
print_r ($keys);

and test $compare for null or 0. for instance

if(empty($compare)) echo "This is empty";

sitehatchery
07-12-2006, 01:32 AM
why are you using strcasecmp() to compare keys?

yunfanny
07-12-2006, 04:40 AM
In fact, as for the statement of "print_r ($wordkeys); ", the output is the strings of array $masterlistfreq, such as : about book car......work worker zone ......;
as for the statement of "print_r ($keys); ", the output is the strings of array $docstemmedwordsfreq, such as : biggest company work.
The values of $compare are numbers which can be positive(e.g., 13, 7, 9,...) or negative (e.g.,-1, -5,...), but none of the values of $compare appreared to be 0, while there is matching of the word "work".

I have tried to use 2 testing conditions:
(1) if (strcasecmp($wordkeys,$keys)==0)
echo "Matching";
else
echo "Not matching";

(2) if ("$wordkeys" === "$keys")
echo "Matching";
else
echo "Not matching";


It turned out to give "Not matching".

yunfanny
07-12-2006, 05:58 AM
the following is a fuller codes:


<?php
//......
//get the strings of $stemmeddocstring from user input
$stemmeddoc=explode(" ",$stemmeddocstring);
$stemmeddoc=removenull($stemmeddoc);
//function removenull is defined in an include file to remove null string
$docstemmedwordsfreq=array_count_values($stemmeddoc);

$materlist_File = "matrix/query.txt";
$masterlist = file_get_contents($materlist_File);
$masterlistArray = explode (" ", $masterlist);
$masterlistArray = removenull ($masterlistArray);

$match=0; //initialise counter for matches
$masterlistfreq=array_count_values($masterlistArray);
foreach ($masterlistfreq as $wordkeys => $wordvalues)
{
foreach ($docstemmedwordsfreq as $keys => $values)
{
$compare = strcmp($wordkeys,$keys);
print_r ($compare); //to test the value of $compare

if (strcasecmp($wordkeys,$keys)==0) { // match found in document
echo "<br>$keys was found in string</br> ";
$match=$match+$values;

$values=$values;

break; // if match found end loop
}
else { // no match found in document
$values=0;
echo "<br>$keys was not found in Doc $c : freq=>0</br> ";
}
}

$querymatch[$wordkeys]=$values; // fill doc arrays with input keywords/freq
$queryvector[]=$values;
}
?>


this time all the values for $compare are -1.

What is wrong with my codes?

Help......

yunfanny
07-13-2006, 10:04 AM
Anyone can help ? :confused:

sitehatchery
07-13-2006, 11:03 AM
strcmp() will return -1 if the first string is less than the second string.

http://www.php.net/manual/en/function.strcmp.php

yunfanny
07-13-2006, 10:31 PM
Hi, I figured it out myself. The $masterlist contains some white space, so it will never be same as any normal words.

Thank you