1

假设我们正在做某种刮,最后我们可以得到重复和半重复的结果。

给定一个可能看起来有点像这样的输入数组:

$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',
  ],
];

我尝试了几种不同的解决方案,但我不喜欢其中任何一种。不管我是怎么做到的,感觉就像是一个杂牌,我觉得我错过了一个明显而优雅的解决方案。

我知道有人会对比我尝试过的排序、循环和过滤更干净的东西提出很好的建议。

4

1 回答 1

2

你可以这样做:

foreach($inputArr as $item) {

    if ( isset($result[$item['title']]) && strlen($result[$item['title']]['desc']) > strlen($item['desc']) )
        continue;    

    $result[$item['title']] = $item;
}

$result = array_values($result);

print_r($result);

演示

您使用标题作为键构建一个新的关联数组。循环原始数组,当键存在时,检查 desc 的长度是否更长,否则继续,将结果数组中的项目替换为当前项目。

您还可以使用array_reduce

$result = array_reduce($inputArr, function ($c, $i) {
    if ( !isset($c[$i['title']]) || strlen($c[$i['title']]['desc']) < strlen($i['desc']) )
        $c[$i['title']] = $i;

    return $c;
});


$result = array_values($result);

print_r($result); 
于 2018-08-21T20:12:40.813 回答