假设我们正在做某种刮,最后我们可以得到重复和半重复的结果。
给定一个可能看起来有点像这样的输入数组:
$inputArr = [
[
'title' => 'Test0',
'desc' => 'Short Desc',
],
[
'title' => 'Test5',
'desc' => 'Short Desc',
],
[
'title' => 'Test0',
'desc' => 'Much Longer Than Short Desc',
],
[
'title' => 'Test0.5',
'desc' => 'Short Desc',
],
[
'title' => 'Test1',
'desc' => 'Short Desc',
],
[
'title' => 'Test1',
'desc' => 'Much Longer Than Short Desc',
],
[
'title' => 'Test1.5',
'desc' => 'Much Longer Than Short Desc',
],
[
'title' => 'Test3',
'desc' => 'Short Desc',
],
[
'title' => 'Test2',
'desc' => 'Short Desc',
],
[
'title' => 'Test3.75',
'desc' => 'Much Longer Than Short Desc',
],
[
'title' => 'Test3.25',
'desc' => 'Short Desc',
],
[
'title' => 'Test2',
'desc' => 'Much Longer Than Short Desc',
],
[
'title' => 'Test3',
'desc' => 'Much Longer Than Short Desc',
],
[
'title' => 'Test5',
'desc' => 'Much Longer Than Short Desc',
],
[
'title' => 'Test3.5',
'desc' => 'Short Desc',
],
[
'title' => 'Test4',
'desc' => 'Short Desc',
],
[
'title' => 'Test5',
'desc' => 'Much Longer Than Short Desc',
],
[
'title' => 'Test4.5',
'desc' => 'Short Desc',
],
[
'title' => 'Test4',
'desc' => 'Much Longer Than Short Desc',
],
[
'title' => 'Test5',
'desc' => 'Much Longer Than Short Desc',
],
];
结果数组必须仅包含具有 ONEtitle
值实例的数组,其中desc
是最长的字符串值,同时删除除一个之外desc
的所有值,其中字符串长度值与其他值相同。
例如,最终输出应如下所示:
$resultArr = [
[
'title' => 'Test0',
'desc' => 'Much Longer Than Short Desc',
],
[
'title' => 'Test0.5',
'desc' => 'Short Desc',
],
[
'title' => 'Test1',
'desc' => 'Much Longer Than Short Desc',
],
[
'title' => 'Test1.5',
'desc' => 'Much Longer Than Short Desc',
],
[
'title' => 'Test2',
'desc' => 'Much Longer Than Short Desc',
],
[
'title' => 'Test3',
'desc' => 'Much Longer Than Short Desc',
],
[
'title' => 'Test3.25',
'desc' => 'Short Desc',
],
[
'title' => 'Test3.5',
'desc' => 'Short Desc',
],
[
'title' => 'Test3.75',
'desc' => 'Much Longer Than Short Desc',
],
[
'title' => 'Test4',
'desc' => 'Much Longer Than Short Desc',
],
[
'title' => 'Test4.5',
'desc' => 'Short Desc',
],
[
'title' => 'Test5',
'desc' => 'Much Longer Than Short Desc',
],
];
我尝试了几种不同的解决方案,但我不喜欢其中任何一种。不管我是怎么做到的,感觉就像是一个杂牌,我觉得我错过了一个明显而优雅的解决方案。
我知道有人会对比我尝试过的排序、循环和过滤更干净的东西提出很好的建议。