如何删除数组中的重复数据?

时间:2015-06-23 23:31:54

标签: php arrays

我有以下数组:

Array
(
    [0] => Array
        (
            [Import] => Array
                (
                    [product_id] => 1
                    [id] => 1
                    [category_id] => 1
                    [amount] => 50
                    [cost] => 8320
                    [paid] => 0
                    [comment] => transportation and others cost: 100  
                    [created] => 2015-06-22 12:09:20
                )

            [0] => Array
                (
                    [total_sell] => 6
                )

        )

    [1] => Array
        (
            [Import] => Array
                (
                    [product_id] => 2
                    [id] => 2
                    [category_id] => 2
                    [amount] => 15
                    [cost] => 3000
                    [paid] => 0
                    [comment] => 
                    [created] => 2015-06-22 12:10:36
                )

            [0] => Array
                (
                    [total_sell] => 1
                )

        )

    [2] => Array
        (
            [Import] => Array
                (
                    [product_id] => 1
                    [id] => 3
                    [category_id] => 1
                    [amount] => 15
                    [cost] => 2000
                    [paid] => 0
                    [comment] => 
                    [created] => 2015-06-22 12:10:58
                )

            [0] => Array
                (
                    [total_sell] => 6
                )

        )

    [3] => Array
        (
            [Import] => Array
                (
                    [product_id] => 1
                    [id] => 4
                    [category_id] => 1
                    [amount] => 50
                    [cost] => 8000
                    [paid] => 0
                    [comment] => 
                    [created] => 2015-06-23 01:10:10
                )

            [0] => Array
                (
                    [total_sell] => 6
                )

        )

)

我想删除[Import][product_id]的重复条目。所以我的预期结果是:

Array
(
    [0] => Array
        (
            [Import] => Array
                (
                    [product_id] => 1
                    [id] => 1
                    [category_id] => 1
                    [amount] => 50
                    [cost] => 8320
                    [paid] => 0
                    [comment] => transportation and others cost: 100  
                    [created] => 2015-06-22 12:09:20
                )

            [0] => Array
                (
                    [total_sell] => 6
                )

        )

    [1] => Array
        (
            [Import] => Array
                (
                    [product_id] => 2
                    [id] => 2
                    [category_id] => 2
                    [amount] => 15
                    [cost] => 3000
                    [paid] => 0
                    [comment] => 
                    [created] => 2015-06-22 12:10:36
                )

            [0] => Array
                (
                    [total_sell] => 1
                )

        )

)

你会写一个函数来过滤这种类型的数组并产生预期的结果。我一直在谷歌搜索2天但没有运气。

2 个答案:

答案 0 :(得分:3)

这是一个方便的一个班轮应该可以做到这一点:

$unique= array_map("unserialize", array_unique(array_map("serialize", $original)));

如果底层数组不相同,那将无效,在这种情况下我认为你可以这样做:

$unique = array_intersect_key($original ,
              array_unique(
                  array_map(function($item) {
                      return $item['Import']['product_id'];
                  }, $original)
              )
           );

经过测试:http://sandbox.onlinephpfunctions.com/code/8aee5cbd614e0ddd1a03dfaa7e98c72fbbe7d68d

答案 1 :(得分:2)

这是一个快速stable sortreduce,它在linearithmic时间内运行。保留首先遇到的产品ID,并忽略具有重复产品ID的条目。

// Stable sort
sort($in);

// Reduce
$out = array_reduce($in, function(&$acc, &$item){ 
    if($item['Import']['product_id'] !== @$acc[sizeof($acc)-1]['Import']['product_id']) {
       $acc[] = $item;    
    }
    return $acc;
}, []);

演示: http://ideone.com/BP0eUJ

更新:这是一个更好的线性时间算法,使用快速"哈希表"抬头。同样,保留第一个遇到的产品Id,并忽略后续的相同Id。

$out = []; 
$hashTable = [];
foreach($in as $item) {
  $pid = $item['Import']['product_id'];
  if(!isset($hashTable[$pid])) {
    $out[] = $item; 
    $hashTable[$pid] = true;
  }
}

演示: http://ideone.com/5RF0og