dpblwmh5218 2013-10-09 14:33
浏览 53
已采纳

如何使用自定义条件从PHP中的多维数组中删除重复值

It's a similar question to this one, but sligthly different and I found no answers so far. Take a look at this:

Array
(
    [0] => Array
    (
        ['id'] => abc
        ['value'] => XXX
    )

    [1] => Array
    (
        ['id'] => abc
        ['value'] => ooo
    )

    [2] => Array
    (
        ['id'] => abc
        ['value'] => qqq
    )

    [3] => Array
    (
        ['id'] => ghi
        ['value'] => YYY
    )

    [4] => Array
    (
        ['id'] => ghi
        ['value'] => jkl
    )

    [5] => Array
    (
        ['id'] => ghi
        ['value'] => XXX
    )

    [6] => Array
    (
        ['id'] => mno
        ['value'] => pql
    )
)

I want to identify all duplicate values in 2-d arrays, and remove them with a custom condition, as for example in uasort.

For example take the first 3 elements [0], [1], [2]:

I want that XXX wins over the others, so the [1] and [2] will be removed,

Same for [3], [4], and [5]: they have the same id, but YYY wins over XXX and other values.

Since these are data retrieved from a DBMS, one alternative I have is to make N different queries passing the next query the id to exclude, e.g.: "

  1. Find all elements with YYY
  2. Find all elements with XXX but that haven't been already found while looking for YYY
  3. Find all elements without YYY and XXX that haven't been already found while looking for YYY or XXX.

Any help would be appreciated, hope it's all understandable.

Linuxatico

  • 写回答

1条回答 默认 最新

  • doukang7501 2013-10-09 15:41
    关注

    It is hard to give an efficient solution without knowing how complex your custom conditions are. If you know and can define them in advance, then you could do something as simple as:

    // Your custom conditions
    $master = array('abc' => array('XXX'), 'ghi' => array('YYY'));
    
    // $dups = the array in your post
    
    // Group by values
    $values = array();
    foreach ( $dups as $k => $v ) {
        if ( is_array($v) && isset($v['id']) ) {
            $values[$v['id']][$k] = $v['value'];
        }
    }
    
    // If master values exists, use it, otherwise use value given
    $deduped = array();
    foreach ( $values as $k => $v ) {
        if ( isset($master[$k]) ) {
            $deduped[key($v)] = array('id' => $k, 'value' => array_shift(array_intersect($master[$k], $v)));
        } else {
            $deduped[key($v)] = array('id' => $k, 'value' => array_shift($v));
        }
    }
    

    Which gives you...

    Array
    (
        [0] => Array
            (
                [id] => abc
                [value] => XXX
            )
    
        [3] => Array
            (
                [id] => ghi
                [value] => YYY
            )
    
        [6] => Array
            (
                [id] => mno
                [value] => pql
            )
    
    )
    

    Looping in PHP can be much quicker than MySQL in many circumstances, so you would need to test using actual data to see if looping is quicker than separate queries.

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

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的