2

抱歉英语不是我的母语,可能问题标题不太好。我想做这样的事情。

$str = array("Lincoln Crown","Crown Court","go holiday","house fire","John Hinton","Hinton Jailed");

这是一个数组,“Lincoln Crown”包含“Lincoln”和“Crown”,因此删除包含这两个词的下一个词,并且“Crown Court(contain Crown)”已被删除。

在另一种情况下。“John Hinton”包含“John”和“Hinton”,因此“Hinton Jailed(contain Hinton)”已被删除。最终输出应该是这样的:

$output = array("Lincoln Crown","go holiday","house fire","John Hinton");

因为我的php技术不好,不是简单的用array_unique()array_diff(),所以开个问题求助,谢谢。

4

6 回答 6

2

似乎您需要一个循环,然后在数组中构建一个单词列表。

喜欢:

<?
// Store existing array's words; elements will compare their words to this array
// if an element's words are already in this array, the element is deleted
// else the element has its words added to this array
$arrayWords = array();

// Loop through your existing array of elements
foreach ($existingArray as $key => $phrase) {
    // Get element's individual words
    $words = explode(" ", $phrase);

    // Assume the element will not be deleted
    $keepWords = true;

    // Loop through the element's words
    foreach ($words as $word) {
        // If one of the words is already in arrayWords (another element uses the word)
        if (in_array($word, $arrayWords)) {
            // Delete the element
            unset($existingArray[$key]);

            // Indicate we are not keeping any of the element's words
            $keepWords = false;

            // Stop the foreach loop
            break;
        }
    }

    // Only add the element's words to arrayWords if the entire element stays
    if ($keepWords) {
        $arrayWords = array_merge($arrayWords, $words);
    }
}
?>
于 2013-04-25T18:13:48.410 回答
2

我认为这可能有效:P

function cool_function($strs){
    // Black list
    $toExclude = array();

    foreach($strs as $s){
        // If it's not on blacklist, then search for it
        if(!in_array($s, $toExclude)){
            // Explode into blocks
            foreach(explode(" ",$s) as $block){
                // Search the block on array
                $found = preg_grep("/" . preg_quote($block) . "/", $strs);
                foreach($found as $k => $f){
                    if($f != $s){
                        // Place each found item that's different from current item into blacklist
                        $toExclude[$k] = $f;
                    }
                }
            }
        }
    }

    // Unset all keys that was found
    foreach($toExclude as $k => $v){
        unset($strs[$k]);
    }

    // Return the result
    return $strs;
}

$strs = array("Lincoln Crown","Crown Court","go holiday","house fire","John Hinton","Hinton Jailed");
print_r(cool_function($strs));

倾倒:

Array
(
    [0] => Lincoln Crown
    [2] => go holiday
    [3] => house fire
    [4] => John Hinton
)
于 2013-04-25T18:25:03.883 回答
0

正如我在你的情况下所做的那样:

$words = array();

foreach($str as $key =>$entry)
{
   $entryWords = explode(' ', $entry);
   $isDuplicated = false;
   foreach($entryWords as $word)
        if(in_array($word, $words))
            $isDuplicated = true;
   if(!$isDuplicated)
        $words = array_merge($words, $entryWords);
   else
        unset($str[$key]);
}

var_dump($str);

输出:

array (size=4)
  0 => string 'Lincoln Crown' (length=13)
  2 => string 'go holiday' (length=10)
  3 => string 'house fire' (length=10)
  4 => string 'John Hinton' (length=11)
于 2013-04-25T18:21:37.527 回答
0

我可以想象有很多技术可以提供您想要的输出,但是您需要的逻辑在您的问题中定义不明确。我假设整个单词匹配是必需的——所以在任何正则表达式模式中都应该使用单词边界。没有提到区分大小写。我不确定是否只有完全唯一的元素(多词字符串)应该将它们的词输入黑名单。我将提供一些片段,但选择适当的技术将取决于确切的逻辑要求。

演示

$output = [];
$blacklist = [];
foreach ($input as $string) {
    if (!$blacklist || !preg_match('/\b(?:' . implode('|', $blacklist) . ')\b/', $string)) {
        $output[] = $string;
    }
    foreach(explode(' ', $string) as $word) {
        $blacklist[$word] = preg_quote($word);
    }
}
var_export($output);

演示

$output = [];
$blacklist = [];
foreach ($input as $string) {
    $words = explode(' ', $string);
    foreach ($words as $word) {
        if (in_array($word, $blacklist)) {
            continue 2;
        }
    }
    array_push($blacklist, ...$words);
    $output[] = $string;
}
var_export($output);

我最喜欢它,因为它在父循环中执行最少的迭代,更紧凑,并且不需要黑名单数组的声明/维护。

演示

$output = [];
while ($input) {
    $output[] = $words = array_shift($input);
    $input = preg_grep('~\b(?:\Q' . str_replace(' ', '\E|\Q', $words) . '\E)\b~', $input, PREG_GREP_INVERT); 
}
var_export($output);
于 2019-06-29T12:35:12.820 回答
-1

您可以分解原始数组中的每个字符串,然后使用循环比较每个单词(将一个数组中的每个单词与另一个数组中的每个单词进行比较,如果它们匹配,则删除整个数组)。

于 2013-04-25T18:11:35.377 回答
-1

array_unique()示例

<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>

输出:

Array
(
    [a] => green
    [0] => red
    [1] => blue
)

资源

于 2015-08-21T18:03:26.397 回答