【问题标题】:How can I remove / filter array elements based on duplicate substrings in associative array in php?如何根据 php 中关联数组中的重复子字符串删除/过滤数组元素?
【发布时间】:2017-01-31 10:06:32
【问题描述】:

我想删除类似的基于标题的值,例如。如果我有蕾哈娜 - 工作英尺。其他一些词和蕾哈娜 - 工作我只想拥有其中一个。我怎样才能删除重复仍然搜索蕾哈娜。请参阅下面包含类似标题的 json:

表示我不想在我的数组中有多个版本的歌曲 请参阅下面的示例 JSON,以作为单一版本过滤掉

    {
      "videos": [
        {
          "kind": "youtube#playlistItem",
          "etag": "\"gMxXHe-zinKdE9lTnzKu8vjcmDI/134M9maQodDR9PapI2tdE24XHdU\"",
          "id": "UExwWEExSXFCZ2VaUXpYOFh2Y0U0R0RscEFpTjAzczNGNi5EQUE1NTFDRjcwMDg0NEMz",
          "snippet": {
            "publishedAt": "2016-07-03T16:45:08.000Z",
            "channelId": "UCOb0YwX9e9SFbctQaSXkKGQ",
            "title": "Rihanna - Work ft. Drake (Audio)",
           
          },
          "shuffle_id": 88
        },
        {
          "kind": "youtube#playlistItem",
          "etag": "\"gMxXHe-zinKdE9lTnzKu8vjcmDI/Qeo1vUZh73p7gX3EFvVxRGbTxms\"",
          "id": "UExaOW5LbUs1dVVCcnN2Rld6ZDRWcFA0MHZ3NlZhLXZFeS5ENDU4Q0M4RDExNzM1Mjcy",
          "snippet": {
            "publishedAt": "2016-08-31T04:42:26.000Z",
            "channelId": "UC2mUsMtec7AOG9K-4ZlO7gA",
            "title": "Rihanna - Work (Explicit) ft. Drake",
            "description": "",
            "channelTitle": "Dickinson Kenneth",
            "playlistId": "PLZ9nKmK5uUBrsvFWzd4VpP40vw6Va-vEy",
            "position": 17,
          
          },
          "shuffle_id": 219
        }]
	}

【问题讨论】:

  • 正如您通过元素的不同值看到的那样,它们是不同的,因此两个轨道对于程序是不同的。现在您可能需要根据自己实现一个逻辑,使其相似,并在此基础上进行过滤
  • 这就是我要问的......逻辑
  • 您可以为歌曲标题定义哈希函数。我们的想法是,对于两个不同但相似的歌曲标题,哈希函数会给出相同的结果。
  • HASH 函数逻辑是什么?
  • 我现在正在写它:)

标签: php arrays json filter


【解决方案1】:

因此,您可以定义一个哈希函数,为相似的歌曲标题返回相同的哈希值;然后,您可以根据该哈希值使歌曲列表唯一。

这是一个潜在的哈希函数和一些演示:

$hash1 = hashSongTitle('Rihanna - Work ft. Drake (Audio)');
$hash2 = hashSongTitle('Rihanna - Work (Explicit) ft. Drake');

echo $hash1 . "\n";
echo $hash2 . "\n";

$sameHash = ($hash1 === $hash2);

echo $sameHash ? 'are the same' : 'not not the same';

function hashSongTitle($title)
{
    //get rid of noise words
    $title = str_replace(array('(Explicit)', '(Audio)', '-'), '', $title);

    //collapse consecutive spaces
    $title = preg_replace('#\s{2,}#ims', ' ', $title);

    //get rid of possible white spaces in front or in the back of the string
    $title  = trim($title, "\r\n ");

    return $title;
}

这应该回显:

Rihanna Work ft. Drake
Rihanna Work ft. Drake
are the same

你可以在这里看到它:http://sandbox.onlinephpfunctions.com/code/201b95cdc80f587a0ee377155c5fb6a49475bc89

然后,您可以将歌曲存储在由该哈希值索引的数组中,因此它们变得唯一。

foreach($songList as $song)
{
    $hash = hashSongTitle($song->title);
    $uniqueSongList[$hash] = $song;
}

【讨论】:

  • 谢谢...很好尝试.. 但是 Rihanna - Work ft. Drake(音频)和 Rihanna - Work(显式) ft. Drake 不是硬编码的,它们是从 API 中即时出现的。 . 所以这些可以是任何值
  • 是的,你必须定义所有的噪音词,比如显式或音频
  • 但它们可以是任何类型的 :'( 我只希望 Rihanna 在那里并作为曲目名称工作,而不是使用 Work 的其他曲目
【解决方案2】:

您可以使用similar_text 函数检测相似度,并确定一个阈值来判断两个(或更多)标题是否相似到足以删除其中一个(最短的?)。

如果您需要更准确的结果,这意味着您不仅对常见字母的数量感兴趣,而且对它们的顺序感兴趣,那么您正在寻找最长的公共子字符串问题here is an implementation。在这里,您必须建立一个与比率最大子字符串长度/原始字符串长度相比的阈值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 2023-04-09
    • 2011-07-23
    • 2021-09-18
    • 2011-12-31
    相关资源
    最近更新 更多