【发布时间】:2014-12-07 09:28:16
【问题描述】:
我正在拼命地试图克服以下问题:在一系列句子/新闻标题中,我试图找到那些非常相似的(有 3 或 4 个共同词)并将它们放入新数组。所以,对于这个原始数组/列表:
'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'
结果应该是:
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
)
这是我的代码:
$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++;
}
它会输出这个[尴尬,但与期望的结果有些接近]:
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
)
任何建议将不胜感激!
【问题讨论】:
-
我给你一些有用的链接,对你有帮助。 Highlight the difference between two strings in PHP。您也可以查看PHP manual 中的
similar_text函数。 -
虽然很脏,但是你可以在循环之后在
$titluri上使用array_unique来得到预期的数组? -
@AlbanPommeret,array_unique 不起作用,已经尝试过了。
标签: php arrays sorting duplicates