remove duplicates from array (array unic by key)

前端 未结 7 2231
一生所求
一生所求 2021-02-19 14:33
Array
(
    [0] => Array
        (
            [file] => /var/websites/example.com/assets/images/200px/1419050406e6648e1c766551a0ffc91380fd6ff3406002011-10-233750.         


        
相关标签:
7条回答
  • 2021-02-19 14:34
    <?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);
    
    0 讨论(0)
  • 2021-02-19 14:37

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

    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.

    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;
    }
    
    0 讨论(0)
  • 2021-02-19 14:38

    Take a look at the function array_unique.

    0 讨论(0)
  • 2021-02-19 14:42

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

    <?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;
        }
    }
    
    0 讨论(0)
  • 2021-02-19 14:42

    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:

    $allMd5s = array_map(function($v) {return $v['md5'];}, $array);
    
    $uniqueMd5s = array_unique($md5);
    
    $result = array_intersect_key($arr, $uniqueMd5s);
    
    0 讨论(0)
  • 2021-02-19 14:48
    // 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'];
      }
    }
    
    0 讨论(0)
提交回复
热议问题