The metaphone case could also follow the same structure proposed by Mike for the strict case.
I do not think that an additional similarity function is needed, because the purpose of the metaphone should be to give us a key that is common to words that sound the same.
$array1 = array('India','USA','China');
$array2 = array(
'Indiuh is in east',
'United States of America is USA',
'Gandhi was born in India',
'Made in China'
);
$found = array();
foreach ($array1 as $key => $value) {
$found[$value] = preg_grep('/\b'.$value.'\b/i', $array2);
}
var_export($found);
echo "\n\n";
function meta( $sentence )
{
return implode(' ', array_map('metaphone', explode(' ', $sentence)));
}
$array2meta = array_map('meta', $array2);
foreach ($array1 as $key => $value) {
$valuemeta = meta($value);
$foundmeta[$value] = preg_grep('/\b'.$valuemeta.'\b/', $array2meta);
$foundmeta[$value] = array_intersect_key($array2, $foundmeta[$value]);
}
var_export($foundmeta);
The above code prints out:
array (
'India' =>
array (
2 => 'Gandhi was born in India',
),
'USA' =>
array (
1 => 'United States of America is USA',
),
'China' =>
array (
3 => 'Made in China',
),
)
array (
'India' =>
array (
0 => 'Indiuh is in east',
2 => 'Gandhi was born in India',
),
'USA' =>
array (
1 => 'United States of America is USA',
),
'China' =>
array (
3 => 'Made in China',
),
)