0

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

4

1 回答 1

1

如果不知道您的自定义条件有多复杂,就很难给出有效的解决方案。如果您知道并且可以提前定义它们,那么您可以做一些简单的事情:

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

这给你...

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

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

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

)

在许多情况下,PHP 中的循环可能比 MySQL 快得多,因此您需要使用实际数据进行测试,以查看循环是否比单独的查询更快。

于 2013-10-09T15:41:03.873 回答