且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP删除数组中的重复单词

更新时间:2022-03-17 03:27:44

我认为这可能有效:P

I think this might work :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
)