Sort array1 by array2 with SIMILAR key matches and not EXACT key matches

I would like the song titles in $Array2 to be sorted in the same order of $Array1 without losing the values from $Array2. The values from $Array2 should follow the new order of each key in $Array2. I believe the current function I have provided is a solid start…

I have two arrays (please notice the differences in each):

[ul][li]$Array1 is the user entered data.[/li]
[li]$Array2 is the data looked up on as external source that is SIMILAR to $Array1 but not EXACT.[/li][/ul]

For example…

$Array1 contains:

Array ( [0] => 3oh!3 - Don't Trust me [1] => Taylor Swift - You Belong with me [2] => Sean Kingston - Fire Burning [3] => Green Day - Know Your Enemy [4] => Kelly Clarkson - Gone )

$Array2 contains:

Array ( [Taylor Swift - You Belong With Me] => bbbbbb [Sean Kingston - Fire Burning] => cccccc [Kelly Clarkson - Gone] => eeeeee [3OH!3- Don't Trust Me lyrics] => aaaaaa [Green Day Know Your Enemy Official] => dddddd )

I already have a function started:
[php]$array1 = array(
0 => ‘3oh!3 - Don’t Trust me’,
1 => ‘Taylor Swift - You Belong with me’,
2 => ‘Sean Kingston - Fire Burning’,
3 => ‘Green Day - Know Your Enemy’,
4 => ‘Kelly Clarkson - Gone’,
);

$array2 = array(
‘Taylor Swift - You Belong With Me’ => ‘bbbbbb’,
‘Sean Kingston - Fire Burning’ => ‘cccccc’,
‘Kelly Clarkson - Gone’ => ‘eeeeee’,
‘3OH!3- Don’t Trust Me lyrics’ => ‘aaaaaa’,
‘Green Day Know Your Enemy Official’ => ‘dddddd’
);

// Find matching song titles (case insensitive).
$tmp = array_values(array_uintersect($array1, array_flip($array2), ‘strcasecmp’));

if ( ! empty($tmp) )
{
// Generate the array.
$matches = array_flip(array_uintersect(array_flip($array2), $tmp, ‘strcasecmp’));

print_r($matches);
}
else{
echo ‘No matches found.’;
}[/php]

This will output:

Array ( [Taylor Swift - You Belong With Me] => bbbbbb [Sean Kingston - Fire Burning] => cccccc [Kelly Clarkson - Gone] => eeeeee )

The other 2 matches are not 100% identical. As many have suggested… I should use the similar_text() function to determine how similar two strings are.

** similar_text(); - http://www.php.net/manual/en/function.similar-text.php

However, I am unsure how to apply the similarity check to my current function. I have a function started but I definitely think it’s in the wrong direction.

Here is my similarity check:
[php]function checkSimilar($str1, $str2){
$quotes = array(’/"/’,"/’/");
$replace = array(
“lyrics”,
“download”
);
$removeQuotes = preg_replace($quotes, ‘’, $str2);
$title = str_ireplace($replace, ‘’, $removeQuotes);
similar_text($str1, $str2, $percent);
if($percent > 75){
echo “{$title}
\n”;
}
else{
echo “Error!
\n”;
}
}[/php]

I also don’t know if it would be easier to convert track titles in each array to all lower-case making the similarity a closer match? I can always use CSS to text-transform: capitalize; for displaying it properly on the website.

Thanks in advance for any inquired interest in this topic!

Sponsor our Newsletter | Privacy Policy | Terms of Service