清除数组中的重复值吗?

发布于 2024-10-16 05:14:21 字数 4440 浏览 7 评论 0原文

大家好,

有人可以请帮帮我吗,我已经尝试解决这个问题有一段时间了,现在摸不着头脑。

我正在添加指向 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 对我不起作用,有人可以帮忙吗?

HI all

Can someone please please help me been trying to suss this for a while now scratching my head.

I am adding links to a SESSION array here is my code.

    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;

}

here is the result off my 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
                )

        )

)

so what i am trying to do is loop through that array and show the link but i dont what any duplicate values.

$turn = $_SESSION['value'];

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

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

echo $furl;

}

and this is the result i get from the 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/

So annoying i only want it to show.

 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

Because above doesnt have any duplicate values i have tried so much array_unique hasnt worked for me can someone please help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回忆追雨的时光 2024-10-23 05:14:21

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

$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;

}

You can use an associative array to check if it's already outputted.

$outputted = array();

Use $furl as a key.

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;

}
云裳 2024-10-23 05:14:21

没问题。试试这个:

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

它将从数组 $arr 中删除所有重复的值。

No problem. Try this:

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

It will remove any duplicate values from the array $arr.

泪之魂 2024-10-23 05:14:21

尝试更改

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;
}

try changing

array_reverse($turn)

to

array_unique(array_reverse($turn))

so it looks like this

foreach (array_unique(array_reverse($turn)) as $value) { 
    $furl = "http://example.s3.amazonaws.com/".urlencode($value['tune_name']);
    echo $furl;
}
妄断弥空 2024-10-23 05:14:21

也许你可以使用 array_unique 它会过滤掉所有重复的值,例如:

$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

maybe you can use array_unique it filters out all duplicated values like:

$turn = $_SESSION['value'];

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

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

echo $furl;

}

result should be:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文