从php数组中只过滤重复的url

Moh*_*mer 10 php arrays url filtering duplicates

这是阵列

Array ( 
   [EM Debt] => http://globalevolution.gws.fcnws.com/fs_Overview.html?isin=LU0616502026&culture=en-GB 
   [EM Local Debt] => Will be launched shortly 
   [EM Blended Debt] => Will be launched shortly 
   [Frontier Markets] => http://globalevolution.gws.fcnws.com/fs_Overview.html?isin=LU0501220262 
   [Absolute Return Debt and FX] => Will be launched shortly 
   [Em Debt] => http://globalevolution.gws.fcnws.com/fs_Overview.html?isin=LU0501220262 
) 
Run Code Online (Sandbox Code Playgroud)

如果我使用array_unique()它也会Will be launched shortly从数组中过滤掉.

我只想过滤重复的网址,而不是文字.

更新:

我需要将Array顺序保持不变,只需过滤dupl即可

sev*_*etl 7

好吧,你可以使用array_filter:

$filtered = array_filter($urls, function ($url) {
    static $used = [];

    if (filter_var($url, FILTER_VALIDATE_URL)) {
        return isset($used[$url]) ? false : $used[$url] = true;
    }

    return true;
});
Run Code Online (Sandbox Code Playgroud)

这是演示.


pra*_*t17 5

这是你的答案:

<?php
// taking just example here, replace `$array` with yours
$array = ['http://globalevolution.gws.fcnws.com/fs_Overview.html?isin=LU0616502026&culture=en-GB', 'abc', 'abc', 'http://globalevolution.gws.fcnws.com/fs_Overview.html?isin=LU0616502026&culture=en-GB'];
$url_array = [];
foreach($array as $ele) {
    if(strpos($ele, 'http://') !== false) {
        $url_array[] = $ele;
    } else {
        $string_array[] = $ele;
    }
}

$url_array = array_unique($url_array);
print_r(array_merge($string_array, $url_array));
?>
Run Code Online (Sandbox Code Playgroud)


Kri*_*ofe 5

您可以遍历数组一次以获得结果,在此过程中,您需要使用额外的数组来指示您在结果中保存了哪个URL.

$saved_urls = [];
$result = [];
foreach($array as $k => $v)
{
    if('http://' == substr(trim($v), 0, 7) || 'https://' == substr(trim($v), 0, 8))
    {
        if(!isset($saved_urls[$v]))    // check if the url have saved
        {
            $result[$k] = $v;
            $saved_urls[$v] = 1;
        }
    }else
        $result[$k] = $v;
}
Run Code Online (Sandbox Code Playgroud)