douhao2856
douhao2856
2014-10-17 10:02
浏览 54
已采纳

再一次关于从PHP中的多维数组中删除重复值

After an exhausting search I found some similar problems like the one I would like to solve, but I could not use their answers.

Hier are some very good examples:

How to remove duplicate values from a multi-dimensional array in PHP

How to remove duplicate values from a multi-dimensional array in PHP

How to remove duplicate values from a multi-dimensional array in PHP revisited

This one is the most similar, but the answer somehow doesn't work to me.

php filter array values and remove duplicates from multi dimensional array

The main difference to my issue is that while people are looking for a solution to delete an entire duplicated subarray from the array, I'm trying to delete the subarray when only one $key => $value pair of the subarray is similar to the correspondent pair of another subarray. The other pairs could have different values or even the same.

Here my array:

Array(

[0] => Array
    (
        [0] => AAA
        [1] => 500
    )

[1] => Array //Won't be delete 'cause [0] is different, although [1] is the same.
    (
        [0] => BBB
        [1] => 500
    )

[2] => Array //Will be delete 'cause [0] is the same.
    (
        [0] => AAA
        [1] => 500
    )

[3] => Array //Won't be delete 'cause [0] is different.
    (
        [0] => CCC
        [1] => 820
    )

[4] => Array
    (
        [0] => AAA //Will be delete 'cause [0] is the same. [1] is also different.
        [1] => 774
    )

How could I manage to delete these subarrays so I have following result:

Array(

[0] => Array
    (
        [0] => AAA
        [1] => 500
    )

[1] => Array
    (
        [0] => BBB
        [1] => 500
    )

[2] => Array
    (
        [0] => CCC
        [1] => 820
    )

Many thanks in advance!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duai4379
    duai4379 2014-10-17 10:07
    已采纳

    The easiest solution (in my opinion), instead of using magical array_filter and array_map handicraft which is difficult to maintain, is to write a specialized function yourself:

    $out = [];
    $usedKeys = [];
    foreach ($in as $element)
    {
        list ($a, $b) = $element;
        if (!isset($usedKeys[$a]))
        {
            $usedKeys[$a] = true;
            $out[]= $element;
        }
    }
    var_dump($out);
    

    This will return only elements whose first pair element is unique (taking first one, if multiple entries are available).

    点赞 评论
  • dongzhui2636
    dongzhui2636 2014-10-17 10:10

    This works for me (based on the link provided):

    <?php
    
    $items = array(
        array(
            0 => 'AAA',
            1 => 500,
        ),
        array(
            0 => 'BBB',
            1 => 500,
        ),
        array(
            0 => 'AAA',
            1 => 500,
        ),
        array(
            0 => 'CCC',
            1 => 820,
        ),
        array(
            0 => 'AAA',
            1 => 774,
        ),
    );
    
    
    
    
    $taken = array();
    
    foreach($items as $key => $item) {
        if(!in_array($item[0], $taken)) {
            $taken[] = $item[0];
        } else {
            unset($items[$key]);
        }
    }
    
    
    echo '<pre>'.print_r($items,true).'</pre>';
    

    fiddle link

    点赞 评论

相关推荐