2014-09-11 90 views
1

我想計數並刪除重複數組。爲了重複,整個數組將具有與另一個的鍵和值的匹配。計數並刪除重複數組

Array 
(
    [0] => Array 
     (
      [name] => Superman 
      [time] => 60 
     ) 

    [1] => Array 
     (
      [name] => Superman 
      [time] => 60 
     ) 

    [2] => Array 
     (
      [name] => Superman 
      [time] => 50 
     ) 

    [3] => Array 
     (
      [name] => Superman 
      [time] => 40 
     ) 

    [4] => Array 
     (
      [name] => Superman 
      [time] => 50 
     ) 

    [5] => Array 
     (
      [name] => Superman 
      [time] => 60 
     ) 

) 

分爲:

Array 
(
    [0] => Array 
     (
      [name] => Superman 
      [time] => 60 
      [count] => 3 
     ) 

    [1] => Array 
     (
      [name] => Superman 
      [time] => 50 
      [count] => 2 
     ) 

    [2] => Array 
     (
      [name] => Superman 
      [time] => 40 
     ) 

) 

我遇到this answer that can remove the duplicates,但我掙扎,看我怎麼可能指望他們。

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

哪個是您唯一的密鑰名稱或時間? – 2014-09-11 13:42:15

回答

1

不作爲因爲我想,但它可以完成工作:

function unserialize_unique_count($input, $k = 'count') { 
    $a = []; 
    foreach ($input as $d) { 
     $s = serialize($d); 
     $a[$s] = (isset($a[$s]) ? ($a[$s] + 1) : 1); 
    } 
    foreach ($a as $s => $c) { 
     $a[$s] = unserialize($s) + [ $k => $c ]; 
    } 
    return array_values($a); 
} 

$grouped_with_count = unserialize_unique_count($input); 

工作原理:第一個循環序列化並計數。第二個獨特的合併。上)。

如何使用:將您的多維數組作爲參數#1傳遞。通過一個附加密鑰「count」來保存計數,從而帶給你獨特的體驗。如果您希望count鍵不是「count」,請傳遞函數第二個參數。

+0

在您的示例輸出中,如果計數爲1,則不存在「計數」鍵。我沒有處理這種情況。如果需要,用下列代碼替換反序列化行:'$ a [$ s] = unserialize($ s)+(1 <$ c?[$ k => $ c]:[]);' – bishop 2014-09-11 14:19:47

1

在這種情況下,您也可以使用array_count_values。例如:

$values = array(
    array('name' => 'Superman', 'time' => 60), 
    array('name' => 'Superman', 'time' => 60), 
    array('name' => 'Superman', 'time' => 50), 
    array('name' => 'Superman', 'time' => 40), 
    array('name' => 'Superman', 'time' => 50), 
    array('name' => 'Superman', 'time' => 60), 
); 

// map out into string then unique them 
$uniques = array_map("unserialize", array_unique(array_map("serialize", $values))); 
$count = array_count_values(array_map("serialize", $values)); // map out the arrays then get the counts 

// then to merge the count 
foreach($uniques as &$batch) { 
    foreach($count as $array => $v) { 
     if(unserialize($array) == $batch) { // if this particular key count is equal to this unique array, then push the count 
      $batch['count'] = $v; 
     } 
    } 
} 

echo '<pre>'; 
print_r($uniques); 

Sample Output

0
  1. 獲取陣列及其價值
  2. 就paypal_address唯一鍵
  3. 使用唯一的密鑰創建臨時數組
  4. 商店都在各自的唯一鍵值temp array
  5. 我使用時間作爲唯一鍵。

    $tmpArr = Array(); 
    $cnt = sizeof($arr); 
    for($i=0;$i<$cnt;$i++){ 
         $time = $arr[$i]['time']; 
         if(!is_array($tmpArr[$time])){ 
           $tmpArr[$time] = Array(); 
           $tmpArr[$time]['count'] = 0; 
         } 
         $tmpArr[$time]['time'] = $arr[$i]['time']; 
         $tmpArr[$time]['name'] = $arr[$i]['name']; 
         $tmpArr[$time]['count'] = $tmpArr[$time]['count'] + 1; 
    
    } 
    print_r($tmpArr); 
    

注:根據您的要求製作的代碼更改

1

快速和骯髒的,但你得到正是你要求的數據結構:

$data = array(
    array("name" => "Superman", "time" => 60), 
    array("name" => "Superman", "time" => 60), 
    array("name" => "Superman", "time" => 50), 
    array("name" => "Superman", "time" => 40), 
    array("name" => "Superman", "time" => 50), 
    array("name" => "Superman", "time" => 60), 
); 

// count the occurrences 
$occurrences = array(); 
for ($i = 0, $l = count($data); $i < $l; $i++) { 
    $serialized = serialize($data[$i]); 
    if (!isset($occurrences[$serialized])) { 
     $occurrences[$serialized] = 1; 
    } 
    else { 
     $occurrences[$serialized] = $occurrences[$serialized] + 1; 
    } 
} 

// get the wanted structure 
$uniques = array(); 
foreach ($occurrences as $serialized => $count) { 
    $unserialized = unserialize($serialized); 
    if ($count > 1) { 
     $unserialized['count'] = $count; 
    } 
    $uniques[] = $unserialized; 
} 

print_r($uniques);