在PHP中从数组中删除重复的单词

时间:2017-02-09 20:47:32

标签: php arrays

让我们说,我们有一些类似

的数组
[
"red book",
"red apple",
"socks",
"grey socks",
"red sky",
"red cross" <----- 4th "red", need to remove this element
"green cross",
"blue jeans"
]

所以我需要删除任何包含在整个数组中重复超过3次的单词的数组元素。因此,上面示例的结果可能如下所示:

[
"red book",
"red apple",
"socks",
"grey socks",
"red sky",
 "green cross",
"blue jeans"
]

所以“red”这个词在数组中重复了3次以上。我们必须保持数组中任何单词的出现次数,并删除其他出现的元素。

在我看来,首先用空间符号将整个阵列打包,然后爆炸成单个单词。使用array_count可能会导致结果。但我无法以这种方式完成这个想法。

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

你需要写一个像这样的函数:

function fix_array ($array)
{
    $filtered = array();
    $word_counts = array();

    foreach ($array as $i => $value)
    {
        $words = explode(' ', $value);
        $temp_word_counts = $word_counts;

        foreach ($words as $word) {
            if (array_key_exists($word, $temp_word_counts)){
                if ($temp_word_counts[$word] == 3){
                    continue 2;
                }
            }
            else{
                 $temp_word_counts[$word] = 0;
            }

            $temp_word_counts[$word]++;
        }

        foreach ($words as $word) {
            if (!array_key_exists($word, $word_counts)){
                $word_counts[$word] = 0;
            }
            $word_counts[$word]++;
        }

        $filtered[] = $value;
    }

    return $filtered;
}

$old_array = [
    "red book",
    "red apple",
    "socks",
    "grey socks",
    "red sky",
    "red cross",
    "green cross",
    "blue jeans"
];

$new_array = fix_array($old_array);

答案 1 :(得分:1)

考虑这个例子:

   $arr = array(
        "red book",
        "red apple",
        "socks",
        "grey socks",
        "red sky",
        "red cross",
        "green cross",
        "blue jeans"
    );
    $used_words = array();
    $new_arr = array();

    array_walk($arr, function($val) {
        $matches = array();
        preg_match_all('/\b\w+?\b/', $val, $matches);

        foreach ($matches[0] as $value) {
            isset($GLOBALS['used_words'][$value]) ? $GLOBALS['used_words'][$value] += 1 : $GLOBALS['used_words'][$value] = 1;

            if ($GLOBALS['used_words'][$value] > 3) {
                return;
            }
        }

        $GLOBALS['new_arr'][] = $val;
    });

    print_r($new_arr);