从数组中删除重复的内容(按键删除数组unic)

时间:2022-08-26 14:19:59
Array
(
    [0] => Array
        (
            [file] => /var/websites/example.com/assets/images/200px/1419050406e6648e1c766551a0ffc91380fd6ff3406002011-10-233750.jpg
            [md5] => 42479bee7a304d2318250de2ef1962a9
            [url] => http://example.com/assets/images/200px/1419050406e6648e1c766551a0ffc91380fd6ff3406002011-10-233750.jpg
        )

    [1] => Array
        (
            [file] => /var/websites/example.com/assets/images/200px/21277792606e6648e1c766551a0ffc91380fd6ff3406002011-10-233750.jpg
            [md5] => 42479bee7a304d2318250de2ef1962a9
            [url] => http://example.com/assets/images/200px/21277792606e6648e1c766551a0ffc91380fd6ff3406002011-10-233750.jpg
        )
)

How can I remove md5 key duplicates from above array?

如何从上面的数组中删除md5密钥副本?

7 个解决方案

#1


11  

<?php
$data = array(
  array(
    'md5' => 'alpha',
    'some' => 'value',
  ),
  array(
    'md5' => 'alpha',
    'some' => 'other value',
  ),
  array(
    'md5' => 'bravo',
    'some' => 'third value',
  ),
);
// walk input array
$_data = array();
foreach ($data as $v) {
  if (isset($_data[$v['md5']])) {
    // found duplicate
    continue;
  }
  // remember unique item
  $_data[$v['md5']] = $v;
}
// if you need a zero-based array, otheriwse work with $_data
$data = array_values($_data);

#2


6  

PHP does already have a function to remove duplicate elements from an array. But unfortunately, array_unique does only support string based comparisons:

PHP已经有一个从数组中删除重复元素的函数。但不幸的是,array_unique只支持基于字符串的比较:

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used.

注意:当且仅当(string) $elem1 === (string) $elem2时,两个元素被认为是相等的。换句话说:当字符串表示形式相同时。第一个元素将被使用。

The problem is that any array turned into a string is equal to any other array:

问题是,任何变成字符串的数组都等于任何其他数组:

Arrays are always converted to the string "Array"; […]

数组总是转换为字符串“数组”;[…]

But you can use the uniqueness of array keys to solve this:

但是你可以使用数组键的唯一性来解决这个问题:

$index = array();
foreach ($arr as $key => $item) {
    if (isset($index[$item['md5']])) {
        unset($item[$key]);
    }
    $index[$item['md5']] = TRUE;
}

#3


5  

Here is my final function after your help guys.... Hope it helps somebody in the future...

这是我的最终功能帮助男人....后希望它对未来的人有所帮助……

    $data = array(
          array(
            'md5' => 'alpha',
            'some' => 'value',
          ),
          array(
            'md5' => 'alpha',
            'some' => 'other value',
          ),
          array(
            'md5' => 'bravo',
            'some' => 'third value',
          ),
        );
        // walk input array


        function remove_duplicateKeys($key,$data){

        $_data = array();

        foreach ($data as $v) {
          if (isset($_data[$v[$key]])) {
            // found duplicate
            continue;
          }
          // remember unique item
          $_data[$v[$key]] = $v;
        }
        // if you need a zero-based array
        // otherwise work with $_data
        $data = array_values($_data);
        return $data;
        }

$my_array = remove_duplicateKeys("md5",$data);

#4


1  

Use array_filter(). Quick code test (doesn't necessarily mirror your situation, but you should get the idea):

使用array_filter()。快速代码测试(不一定反映您的情况,但您应该理解):

<?php
header('Content-Type: Text/Plain');
$array = array(
    0 => array('name' => 'samson'),
    1 => array('name' => 'delilah'),
    2 => array('name' => 'samson'),
    3 => array('name' => 'goliath'),
);

$md5Processed = array();

$newArray = array_filter($array, "uniqueMD5");

print_r($newArray);

exit;

function uniqueMD5( $data ){
    global $md5Processed;

    if( !in_array( $data['name'], $md5Processed ) )
    {
        $md5Processed[] = $data['name'];
        return true;
    }
}

#5


1  

// your array
$array = array(...);
// will be used to store md5 hashes
$md5 = array();
// walk through array
foreach ($array as $key => $arr) {
  // have we already seen this md5 hash?
  if (in_array($arr['md5'], $md5)){
    // remove element
    unset($array[$key]);
  }else {
    // keep element, but add it's md5
    $md5[] = $arr['md5'];
  }
}

#6


1  

As array_unique operates on flat arrays, you can not use it directly. But you can first map all 'md5' values to a flat array, unique it and then get the elements with array_intersect_key:

由于array_unique操作平面数组,因此不能直接使用它。但是您可以先将所有的“md5”值映射到一个平面数组中,并将其惟一,然后使用array_intersect_key获取元素:

$allMd5s = array_map(function($v) {return $v['md5'];}, $array);

$uniqueMd5s = array_unique($md5);

$result = array_intersect_key($arr, $uniqueMd5s);

#7


-2  

Take a look at the function array_unique.

看看函数array_unique。

#1


11  

<?php
$data = array(
  array(
    'md5' => 'alpha',
    'some' => 'value',
  ),
  array(
    'md5' => 'alpha',
    'some' => 'other value',
  ),
  array(
    'md5' => 'bravo',
    'some' => 'third value',
  ),
);
// walk input array
$_data = array();
foreach ($data as $v) {
  if (isset($_data[$v['md5']])) {
    // found duplicate
    continue;
  }
  // remember unique item
  $_data[$v['md5']] = $v;
}
// if you need a zero-based array, otheriwse work with $_data
$data = array_values($_data);

#2


6  

PHP does already have a function to remove duplicate elements from an array. But unfortunately, array_unique does only support string based comparisons:

PHP已经有一个从数组中删除重复元素的函数。但不幸的是,array_unique只支持基于字符串的比较:

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used.

注意:当且仅当(string) $elem1 === (string) $elem2时,两个元素被认为是相等的。换句话说:当字符串表示形式相同时。第一个元素将被使用。

The problem is that any array turned into a string is equal to any other array:

问题是,任何变成字符串的数组都等于任何其他数组:

Arrays are always converted to the string "Array"; […]

数组总是转换为字符串“数组”;[…]

But you can use the uniqueness of array keys to solve this:

但是你可以使用数组键的唯一性来解决这个问题:

$index = array();
foreach ($arr as $key => $item) {
    if (isset($index[$item['md5']])) {
        unset($item[$key]);
    }
    $index[$item['md5']] = TRUE;
}

#3


5  

Here is my final function after your help guys.... Hope it helps somebody in the future...

这是我的最终功能帮助男人....后希望它对未来的人有所帮助……

    $data = array(
          array(
            'md5' => 'alpha',
            'some' => 'value',
          ),
          array(
            'md5' => 'alpha',
            'some' => 'other value',
          ),
          array(
            'md5' => 'bravo',
            'some' => 'third value',
          ),
        );
        // walk input array


        function remove_duplicateKeys($key,$data){

        $_data = array();

        foreach ($data as $v) {
          if (isset($_data[$v[$key]])) {
            // found duplicate
            continue;
          }
          // remember unique item
          $_data[$v[$key]] = $v;
        }
        // if you need a zero-based array
        // otherwise work with $_data
        $data = array_values($_data);
        return $data;
        }

$my_array = remove_duplicateKeys("md5",$data);

#4


1  

Use array_filter(). Quick code test (doesn't necessarily mirror your situation, but you should get the idea):

使用array_filter()。快速代码测试(不一定反映您的情况,但您应该理解):

<?php
header('Content-Type: Text/Plain');
$array = array(
    0 => array('name' => 'samson'),
    1 => array('name' => 'delilah'),
    2 => array('name' => 'samson'),
    3 => array('name' => 'goliath'),
);

$md5Processed = array();

$newArray = array_filter($array, "uniqueMD5");

print_r($newArray);

exit;

function uniqueMD5( $data ){
    global $md5Processed;

    if( !in_array( $data['name'], $md5Processed ) )
    {
        $md5Processed[] = $data['name'];
        return true;
    }
}

#5


1  

// your array
$array = array(...);
// will be used to store md5 hashes
$md5 = array();
// walk through array
foreach ($array as $key => $arr) {
  // have we already seen this md5 hash?
  if (in_array($arr['md5'], $md5)){
    // remove element
    unset($array[$key]);
  }else {
    // keep element, but add it's md5
    $md5[] = $arr['md5'];
  }
}

#6


1  

As array_unique operates on flat arrays, you can not use it directly. But you can first map all 'md5' values to a flat array, unique it and then get the elements with array_intersect_key:

由于array_unique操作平面数组,因此不能直接使用它。但是您可以先将所有的“md5”值映射到一个平面数组中,并将其惟一,然后使用array_intersect_key获取元素:

$allMd5s = array_map(function($v) {return $v['md5'];}, $array);

$uniqueMd5s = array_unique($md5);

$result = array_intersect_key($arr, $uniqueMd5s);

#7


-2  

Take a look at the function array_unique.

看看函数array_unique。