清除数组中的重复值?

时间:2011-02-03 10:17:51

标签: php arrays unique

全部

有人可以请帮助我一直试着用这一段时间来讨论这个问题。

我在这里添加了一个SESSION数组的链接是我的代码。

    session_start();
$test = $_GET;
$_SESSION['value'][]=$test;
echo "<pre>";
//print_r($_SESSION);
echo "</pre>";


$turn = $_SESSION['value'];

foreach (array_reverse($turn) as $value) { 

$furl = "http://example.s3.amazonaws.com/".urlencode($value['tune_name']);

echo $furl;

}

这是我的print_r($ _ SESSION);

的结果
Array
(
    [attach] => Array
        (
        )

    [backups-2] => true
    [dnb] => true
    [house] => true
    [tech-house] => true
    [uk-garage] => true
    [uk-grime] => true
    [uk-hip-hop] => true
    [uncategorized] => true
    [warehouse] => true
    [active] => 
    [value] => Array
        (
            [0] => Array
                (
                    [tune_name] => Music/dnb/03 Twerk feat. Yo Majesty (Sub Focus Remix).mp3
                    [tune] => http://example.s3.amazonaws.com/Music/dnb/03 Twerk feat. Yo Majesty (Sub Focus Remix).mp3
                    [submit] => Listen
                )

            [1] => Array
                (
                )

            [2] => Array
                (
                    [tune_name] => Music/dnb/Abort_Delta Heavy_192.mp3
                    [tune] => http://example.s3.amazonaws.com/Music/dnb/Abort_Delta Heavy_192.mp3
                    [submit] => Listen
                )

            [3] => Array
                (
                    [tune_name] => Music/dnb/03 Twerk feat. Yo Majesty (Sub Focus Remix).mp3
                    [tune] => http://example.s3.amazonaws.com/Music/dnb/03 Twerk feat. Yo Majesty (Sub Focus Remix).mp3
                    [submit] => Listen
                )

            [4] => Array
                (
                    [tune_name] => Music/dnb/21st Century (Feat. Cabbie)_Tantrum Desire_192.mp3
                    [tune] => http://example.s3.amazonaws.com/Music/dnb/21st Century (Feat. Cabbie)_Tantrum Desire_192.mp3
                    [submit] => Listen
                )

            [5] => Array
                (
                    [tune_name] => Music/dnb/Abort_Delta Heavy_192.mp3
                    [tune] => http://example.s3.amazonaws.com/Music/dnb/Abort_Delta Heavy_192.mp3
                    [submit] => Listen
                )

            [6] => Array
                (
                )

            [7] => Array
                (
                    [tune_name] => Music/dnb/03 Twerk feat. Yo Majesty (Sub Focus Remix).mp3
                    [tune] => http://example.s3.amazonaws.com/Music/dnb/03 Twerk feat. Yo Majesty (Sub Focus Remix).mp3
                    [submit] => Listen
                )

            [8] => Array
                (
                    [tune_name] => Music/dnb/Abort_Delta Heavy_192.mp3
                    [tune] => http://example.s3.amazonaws.com/Music/dnb/Abort_Delta Heavy_192.mp3
                    [submit] => Listen
                )

        )

)

所以我想要做的是循环遍历该数组并显示链接,但我不重复任何值。

$turn = $_SESSION['value'];

foreach (array_reverse($turn) as $value) { 

$furl = "http://example.s3.amazonaws.com/".urlencode($value['tune_name']);

echo $furl;

}

这是我从foreach得到的结果。

http://example.s3.amazonaws.com/Music%2Fdnb%2FAbort_Delta+Heavy_192.mp3
http://example.s3.amazonaws.com/Music%2Fdnb%2FAbort_Delta+Heavy_192.mp3
http://example.s3.amazonaws.com/Music%2Fdnb%2F03+Twerk+feat.+Yo+Majesty+%28Sub+Focus+Remix%29.mp3
http://example.s3.amazonaws.com/
http://example.s3.amazonaws.com/Music%2Fdnb%2FAbort_Delta+Heavy_192.mp3
http://example.s3.amazonaws.com/Music%2Fdnb%2F21st+Century+%28Feat.+Cabbie%29_Tantrum+Desire_192.mp3
http://example.s3.amazonaws.com/Music%2Fdnb%2F03+Twerk+feat.+Yo+Majesty+%28Sub+Focus+Remix%29.mp3
http://example.s3.amazonaws.com/Music%2Fdnb%2FAbort_Delta+Heavy_192.mp3
http://example.s3.amazonaws.com/

太烦人了我只想表现出来。

 http://example.s3.amazonaws.com/Music%2Fdnb%2FAbort_Delta+Heavy_192.mp3
http://example.s3.amazonaws.com/Music%2Fdnb%2F03+Twerk+feat.+Yo+Majesty+%28Sub+Focus+Remix%29.mp3
http://example.s3.amazonaws.com/Music%2Fdnb%2F21st+Century+%28Feat.+Cabbie%29_Tantrum+Desire_192.mp3

因为上面没有任何重复的值我尝试了这么多array_unique对我来说没有用,有人可以帮忙吗?

4 个答案:

答案 0 :(得分:0)

您可以使用关联数组来检查它是否已输出。

$outputted = array();

使用$furl作为密钥。

foreach (array_reverse($turn) as $value) { 

    $furl = "http://example.s3.amazonaws.com/".urlencode($value['tune_name']);

    if (isset($outputted[$furl])) {
        continue;
    }

    echo $furl;

    $outputted[$furl] = true;

}

答案 1 :(得分:0)

没问题。试试这个:

function filter($val)
{
    static $base=array();
    if(in_array($val,$base))return false;
    $base[]=$val;
    return true;
}
array_filter($arr,"filter");

它将从数组$ arr。

中删除任何重复值

答案 2 :(得分:0)

尝试更改

array_reverse($turn)

array_unique(array_reverse($turn))

所以它看起来像这样

foreach (array_unique(array_reverse($turn)) as $value) { 
    $furl = "http://example.s3.amazonaws.com/".urlencode($value['tune_name']);
    echo $furl;
}

答案 3 :(得分:0)

也许您可以使用array_unique过滤掉所有duplicated值,例如:

$turn = $_SESSION['value'];

foreach (array_unique(array_reverse($turn)) as $value) { 

$furl = "http://example.s3.amazonaws.com/".urlencode($value['tune_name']);

echo $furl;

}

结果应为:

http://example.s3.amazonaws.com/Music%2Fdnb%2FAbort_Delta+Heavy_192.mp3
http://example.s3.amazonaws.com/Music%2Fdnb%2F03+Twerk+feat.+Yo+Majesty+%28Sub+Focus+Remix%29.mp3
http://example.s3.amazonaws.com/Music%2Fdnb%2F21st+Century+%28Feat.+Cabbie%29_Tantrum+Desire_192.mp3