Your PHP code threw an error.
By way of a demonstration that this can work:
PHP Code:
<?php
$string1 = "foo,baar,checkers,cricket,games,running";
$string2 = "foo,running,tennis,games";
$array1 = explode( ',', $string1 );
$array2 = explode( ',', $string2 );
$commonSkills = array_intersect($array1,$array2); // checks for intersection
$countar = array_count_values($commonSkills); // count for arrays
$count = 0;
foreach($countar as $values)
{
$count += $values;
}
echo $count;
?>
But to get the count, you don't have to iterate over the array_count_values array, unless you want to count multiple matches of the same item (an item that repeats in the first array and matches at least one item in the other).
You can just do:
PHP Code:
<?php
$string1 = "foo,baar,checkers,cricket,games,running";
$string2 = "foo,running,tennis,games";
$array1 = explode( ',', $string1 );
$array2 = explode( ',', $string2 );
$commonSkills = array_intersect($array1,$array2); // checks for intersection
$countar = array_count_values($commonSkills); // count for arrays
echo count($countar);
?>
Which would perhaps be more accurate. Depends what you're after.
If you do want duplicates counted, even simpler would be:
PHP Code:
<?php
$string1 = "foo,baar,checkers,foo,cricket,games,running";
$string2 = "foo,running,tennis,running,games";
$array1 = explode( ',', $string1 );
$array2 = explode( ',', $string2 );
$commonSkills = array_intersect($array1,$array2); // checks for intersection
echo count($commonSkills);
?>
If there's white space in your strings after or brfore the commas and it's not uniform, see:
http://www.php.net/manual/en/function.preg-split.php
Bookmarks