Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter just duplicate urls from an php array

Here is an array

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 
) 

If I use array_unique() it will filter Will be launched shortly from the array also.

I just want to filter duplicate urls, not text.

UPDATE:

I need to be Array order remain as same, Just filter dupl

like image 696
Mohammad Umer Avatar asked Dec 08 '17 12:12

Mohammad Umer


2 Answers

Well, you can use 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;
});

Here is demo.

like image 137
sevavietl Avatar answered Oct 17 '22 09:10

sevavietl


Here is your answer:

<?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));
?>
like image 45
pravindot17 Avatar answered Oct 17 '22 08:10

pravindot17