按值计算的PHP唯一数组


PHP unique array by value?

我在PHP中有一个数组,如下所示:

  [0]=>
       array(2) {
           ["name"]=>
              string(9) "My_item"
           ["url"]=>
              string(24) "http://www.my-url.com/"
       }
  [1]=>
     array(2) {
         ["name"]=>
             string(9) "My_item"
         ["url"]=>
            string(24) "http://www.my-url2.com/"
     }

"name"中的两个值在这两项中是相同的。我想把重复的东西整理成这样。

如何通过检查"name"值来创建唯一数组?

基本上是

$unique_array = [];
foreach($your_array as $element) {
    $hash = $element[field-that-should-be-unique];
    $unique_array[$hash] = $element;
}
$result = array_values($unique_array);

序列化对于简化建立层次数组唯一性的过程非常有用。使用这一行来检索只包含唯一元素的数组。

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

请发现此链接很有用,使用md5哈希检查重复项:

http://www.phpdevblog.net/2009/01/using-array-unique-with-multidimensional-arrays.html

快速一瞥:

/**
 * Create Unique Arrays using an md5 hash
 *
 * @param array $array
 * @return array
 */
function arrayUnique($array, $preserveKeys = false)
{
    // Unique Array for return
    $arrayRewrite = array();
    // Array with the md5 hashes
    $arrayHashes = array();
    foreach($array as $key => $item) {
        // Serialize the current element and create a md5 hash
        $hash = md5(serialize($item));
        // If the md5 didn't come up yet, add the element to
        // to arrayRewrite, otherwise drop it
        if (!isset($arrayHashes[$hash])) {
            // Save the current element hash
            $arrayHashes[$hash] = $hash;
            // Add element to the unique Array
            if ($preserveKeys) {
                $arrayRewrite[$key] = $item;
            } else {
                $arrayRewrite[] = $item;
            }
        }
    }
    return $arrayRewrite;
}
$uniqueArray = arrayUnique($array);
var_dump($uniqueArray);

请参阅此处的工作示例:http://codepad.org/9nCJwsvg

简单解决方案:

/**
 * @param $array
 * @param null $key
 * @return array
 */
public static function unique($array,$key = null){
    if(null === $key){
        return array_unique($array);
    }
    $keys=[];
    $ret = [];
    foreach($array as $elem){
        $arrayKey = (is_array($elem))?$elem[$key]:$elem->$key;
        if(in_array($arrayKey,$keys)){
            continue;
        }
        $ret[] = $elem;
        array_push($keys,$arrayKey);
    }
    return $ret;
}
function unique_multidim_array($array, $key) { 
            $temp_array = array(); 
            $i = 0; 
            $key_array = array(); 
            foreach($array as $val) { 
                if (!in_array($val[$key], $key_array)) { 
                    $key_array[$i] = $val[$key]; 
                    $temp_array[$i] = $val; 
                } 
                $i++; 
            } 
            return $temp_array; 
        } 
$result = unique_multidim_array($visitors,'ip');

给定数组(0,1)上的键似乎并不重要,一个简单的解决方案是使用"name"引用的元素的值作为外部数组的键:

["My_item"]=>
   array(2) {
       ["name"]=>
          string(9) "My_item"
       ["url"]=>
          string(24) "http://www.my-url.com/"
   }

如果除了"name"之外只有一个值,为什么还要使用嵌套数组呢?

["My_item"]=>"http://www.my-url.com/"