从数组中删除重复项

时间:2010-08-26 21:38:26

标签: php

我有一个多维数组,如下所示:

Array
(
    [0] => Array
        (
            [id] => 1
            [genretitel] => actie
            [weekdag] => zaterdag
            [zaaltitel] => zaal 1
        )

    [1] => Array
        (
            [id] => 5
            [genretitel] => actie
            [weekdag] => zaterdag
            [zaaltitel] => zaal 1
        )

    [2] => Array
        (
            [id] => 2
            [genretitel] => animatie
            [weekdag] => zaterdag
            [zaaltitel] => zaal 1
        )

    [3] => Array
        (
            [id] => 6
            [genretitel] => komedie
            [weekdag] => zaterdag
            [zaaltitel] => zaal 1
        )

    [4] => Array
        (
            [id] => 4
            [genretitel] => animatie
            [weekdag] => zaterdag
            [zaaltitel] => zaal 2
        )

    [5] => Array
        (
            [id] => 3
            [genretitel] => drama
            [weekdag] => zaterdag
            [zaaltitel] => zaal 2
        )

    [6] => Array
        (
            [id] => 8
            [genretitel] => science fiction
            [weekdag] => zaterdag
            [zaaltitel] => zaal 2
        )

    [7] => Array
        (
            [id] => 11
            [genretitel] => avontuur
            [weekdag] => zaterdag
            [zaaltitel] => zaal 2
        )

    [8] => Array
        (
            [id] => 15
            [genretitel] => actie
            [weekdag] => zaterdag
            [zaaltitel] => zaal 3
        )

    [9] => Array
        (
            [id] => 13
            [genretitel] => animatie
            [weekdag] => zaterdag
            [zaaltitel] => zaal 3
        )

    [10] => Array
        (
            [id] => 7
            [genretitel] => documentaire
            [weekdag] => zaterdag
            [zaaltitel] => zaal 3
        )

    [11] => Array
        (
            [id] => 10
            [genretitel] => science fiction
            [weekdag] => zaterdag
            [zaaltitel] => zaal 3
        )

    [12] => Array
        (
            [id] => 12
            [genretitel] => actie
            [weekdag] => zaterdag
            [zaaltitel] => zaal 4
        )

    [13] => Array
        (
            [id] => 14
            [genretitel] => animatie
            [weekdag] => zaterdag
            [zaaltitel] => zaal 4
        )

    [14] => Array
        (
            [id] => 9
            [genretitel] => drama
            [weekdag] => zaterdag
            [zaaltitel] => zaal 4
        )

    [15] => Array
        (
            [id] => 16
            [genretitel] => historisch
            [weekdag] => zaterdag
            [zaaltitel] => zaal 4
        )

)

我想拥有的只是相同的多维数组,但没有重复数据。

1 个答案:

答案 0 :(得分:2)

看起来您正在从数据库中获取数据。在这种情况下,我建议从select中删除ID并使用类似DISTINCT的内容,以避免从数据库中读取重复项。

编辑:如果您真的无法避免在选择中使用ID,请尝试以下操作:

// remove the id from all subarrays
foreach($array as $key => $value){
    unset($array[$key]["id"]);
}
// remove all duplictes
$array = array_unique($array);

编辑:所以如果你完全想要将所有数据从数组中分离出来,那么三个数组就会出现任何相互对应的数据,只需迭代数组并制作三个数组并使用独特的函数算账:

// create an array for each key
foreach($array as $value){
    $genretitel[] = $value["genretitel"];
    $weekdag[] = $value["weekdag"];
    $zaaltitel[] = $value["zaaltitel"];
}

// remove the duplicates for thos arrays
$genretitel = array_unique($genretitel);
$weekdag= array_unique($weekdag);
$zaaltitel= array_unique($zaaltitel);

如果这甚至不是您所询问的内容,请在此过程后添加所需数据的示例。