remove duplicates from array (array unic by key)

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


        
相关标签:
7条回答
  • 2021-02-19 14:51

    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);
    
    0 讨论(0)
提交回复
热议问题