duankousong9637 2010-08-26 21:38
浏览 13
已采纳

从数组中删除重复项

I have a multidimensional array looking like this:

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
        )

)

What I would like to have is just the same multidimensional array but without the duplicates.

  • 写回答

1条回答 默认 最新

  • dongyang1518 2010-08-26 22:17
    关注

    It looks like you are getting the data from a data base. In that case I would recommend to remove the ID from the select and use something like DISTINCT to avoid duplicates read from the data base.

    EDIT: If you really couldn't avoid to have the ID in the select, try the following:

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

    EDIT: So if you completely want to separate all of the data from the array, to three arrays whitout any corresponing to each other just iterate over the array and make three arrays and use the unique function afterwards:

    // 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);
    

    If that is even not what you are asking about, please add a example of your desired data after the process.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图