这是其他几篇文章的部分重复,但略有不同。和我一起裸露一下,请原谅我是否已经在StackOverflow的纪事中讨论过。
下面是来自两个不同供稿的两个数组,它们共享不同的ID。因此,我必须依靠'BriefTitle':通过'BriefTitle'和其他数据(例如[LocationCountry],[StartDate],[Condition])可以知道这是同一记录。我想对'BriefTitle'进行比较以将其与其他'BriefTitle'记录进行比较,以过滤出重复项,因为它们包含在彼此中。我不是在寻找精确匹配,这是我在这里找到大多数解决方案的目的。
我喜欢sevavietl / mickmackusa提出的简短解决方案:php remove duplicates from multidimensional array by value
$result = array_reverse(array_values(array_column(
array_reverse($data),
null,
'BriefTitle'
)));
但是,我的“ BriefTitle”是一个数组(似乎不适用于array_column),并且我不确定如何将substr函数应用于上述解决方案。
一些注意事项:
任何想法我应该如何处理?数组:
[0] => Array
(
[Rank] => 422
[id] => Array
(
[0] => 152091
)
[Condition] => Array
(
[0] => Depression
[1] => Ketamine
)
[BriefTitle] => Array
(
[0] => Positron Emission Tomography Assessment of Ketamine Binding of the Serotonin Transporter
)
[LocationCountry] => Array
(
[0] => Austria
)
[StartDate] => Array
(
[0] => May 5, 2016
)
[LastUpdatePostDate] => Array
(
[0] => October 15, 2018
)
[Entheogen] => ketamine
[Source] => clinicaltrials.gov
)
[1] => Array
(
[Rank] => 6673
[id] => Array
(
[0] => YSBSZ18291
)
[Condition] => Array
(
[0] => Depressive Disorder
[1] => Ketamine
)
[BriefTitle] => Array
(
[0] => Positron Emission Tomography assessment of Ketamine Binding of the Serotonin Transporter and its Relevance for Rapid Antidepressant Response
[1] => Die Rolle des Serotonintransporters bei der akuten antidepressiven Wirkung von Ketamin, untersucht mit Positronen-Emissions-Tomographie
)
[LocationCountry] => Array
(
[0] => Austria
)
[StartDate] => Array
(
[0] => 2016 05 01
)
[LastUpdatePostDate] => Array
(
[0] => 2018 10 15
)
[Entheogen] => ketamine
[Source] => clinicaltrialsregister.eu
)
不幸的是,由于数据的性质(匹配的字符串可能是其他字符串的子字符串,具有不同的大小写),唯一的实际选择是强制执行此操作。循环遍历数组,随时存储标题,并检查当前标题是否与其中任何一个匹配:
$result = array();
$brieftitles = array();
foreach ($array as $arr) {
$foundtitle = false;
$title = $arr['BriefTitle'][0];
foreach ($brieftitles as $btitle) {
$foundtitle = (stripos($title, $btitle) !== false) || (stripos($btitle, $title) !== false);
if ($foundtitle) break;
}
if (!$foundtitle) {
$result[] = $arr;
$brieftitles[] = $arr['BriefTitle'][0];
}
}
print_r($result);