I'm desperately trying to overcome the following issue: out of an array of sentences/news titles, I'm trying to find those which are very similar (have some 3 or 4 words in common) and put them into a new array. So, for this original array/list:
'Title1: Hackers expose trove of snagged Snapchat images',
'Title2: New Jersey officials order symptom-less NBC News crew into Ebola quarantine',
'Title3: Family says goodbye at funeral for 16-year-old',
'Title4: New Jersey officials talk about Ebola quarantine',
'Title5: New Far Cry 4 Trailer Welcomes You to Kyrat Lowlands',
'Title6: Hackers expose Snapchat images'
The result should be:
Array
(
[0] => Title1: Hackers expose trove of snagged Snapchat images
[1] => Array
(
[duplicate] => Title6: Hackers expose Snapchat images
)
[2] => Title2: New Jersey officials order symptom-less NBC News crew into Ebola quarantine
[3] => Array
(
[duplicate] => Title4: New Jersey officials talk about Ebola quarantine
)
[4] => Title3: Family says goodbye at funeral for 16-year-old
[5] => Title5: New Far Cry 4 Trailer Welcomes You to Kyrat Lowlands
)
This is my code:
$titles = array(
'Title1: Hackers expose trove of snagged Snapchat images',
'Title2: New Jersey officials order symptom-less NBC News crew into Ebola quarantine',
'Title3: Family says goodbye at funeral for 16-year-old',
'Title4: New Jersey officials talk about Ebola quarantine',
'Title5: New Far Cry 4 Trailer Welcomes You to Kyrat Lowlands',
'Title6: Hackers expose Snapchat images'
);
$z = 1;
foreach ($titles as $feed)
{
$feed_A = explode(' ', $feed);
for ($i=$z; $i<count($titles); $i++)
{
$feed_B = explode(' ', $titles[$i]);
$intersect_A_B = array_intersect($feed_A, $feed_B);
if(count($intersect_A_B)>3)
{
$titluri[] = $feed;
$titluri[]['duplicate'] = $titles[$i];
}
else
{
$titluri[] = $feed;
}
}
$z++;
}
It outputs this [awkward, but somewhat colse to the desired] result:
Array
(
[0] => Title1: Hackers expose trove of snagged Snapchat images
[1] => Title1: Hackers expose trove of snagged Snapchat images
[2] => Title1: Hackers expose trove of snagged Snapchat images
[3] => Title1: Hackers expose trove of snagged Snapchat images
[4] => Title1: Hackers expose trove of snagged Snapchat images
[5] => Array
(
[duplicate] => Title6: Hackers expose Snapchat images
)
[6] => Title2: New Jersey officials order symptom-less NBC News crew into Ebola quarantine
[7] => Title2: New Jersey officials order symptom-less NBC News crew into Ebola quarantine
[8] => Array
(
[duplicate] => Title4: New Jersey officials talk about Ebola quarantine
)
[9] => Title2: New Jersey officials order symptom-less NBC News crew into Ebola quarantine
[10] => Title2: New Jersey officials order symptom-less NBC News crew into Ebola quarantine
[11] => Title3: Family says goodbye at funeral for 16-year-old
[12] => Title3: Family says goodbye at funeral for 16-year-old
[13] => Title3: Family says goodbye at funeral for 16-year-old
[14] => Title4: New Jersey officials talk about Ebola quarantine
[15] => Title4: New Jersey officials talk about Ebola quarantine
[16] => Title5: New Far Cry 4 Trailer Welcomes You to Kyrat Lowlands
)
Any suggestions would be much appreciated!