OSDN Git Service

array_unique_recursive()
authorhenoheno <henoheno>
Sat, 5 May 2007 04:59:31 +0000 (13:59 +0900)
committerhenoheno <henoheno>
Sat, 5 May 2007 04:59:31 +0000 (13:59 +0900)
spam/SpamTest.php
spam/spam.php

index df98e95..ec3b4e7 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: SpamTest.php,v 1.8 2007/05/04 14:54:21 henoheno Exp $
+// $Id: SpamTest.php,v 1.9 2007/05/05 04:59:31 henoheno Exp $
 // Copyright (C) 2007 heno
 //
 // Design test case for spam.php (called from runner.php)
@@ -76,8 +76,38 @@ class SpamTest extends PHPUnit_TestCase
                $this->assertEquals(19, array_count_leaves($array, TRUE));
        }
 
+       function testPhpFunc_array_unique()
+       {
+               $this->assertEquals(array(1), array_unique(array(1, 1)));
+
+               // Keys are preserved, array()s inside are preserved
+               $this->assertEquals(
+                       array(0, 2 => array(1, 1), 3 => 2),
+                       array_unique(
+                               array(0, 0, array(1, 1), 2, 2)
+                       )
+               );
+
+               // Keys are preserved
+               $this->assertEquals(
+                       array(0, 2 => array(1, 1), 3 => 2),
+                       array_unique(array(0, 0, array(1, 1), 2, 2))
+               );
+
+               // ONLY the first array() is preserved
+               $this->assertEquals(
+                       array(0 => array(1, 1)),
+                       array_unique(array_unique(array(0 => array(1, 1), 'a' => array(2,2), 'b' => array(3, 3))))
+               );
+       }
+
        function testFunc_array_merge_leaves()
        {
+               $array1 = array(1);
+               $array2 = array(1);
+               $result = array(1);
+               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+
                $array1 = array(2);
                $array2 = array(1);
                $result = array(2, 1);
@@ -138,18 +168,16 @@ class SpamTest extends PHPUnit_TestCase
                );
                $this->assertEquals($result, array_merge_leaves($array1, $array2));
 
-               // ----
-
-               // Values will not be unique now
+               // Values will not be unique?
                $array1 = array(5, 4);
                $array2 = array(4, 5);
-               $result = array(5, 4, 4, 5);
+               $result = array(5, 4);
                $this->assertEquals($result, array_merge_leaves($array1, $array2));
 
                // One more thing ...
                $array1 = array('b' => array('k3'));
                $array2 = array('b' => 'k3');
-               $result = array('b' => array('k3', 'k3'));
+               $result = array('b' => array('k3'));
                $this->assertEquals($result, array_merge_leaves($array1, $array2));
        }
 
index 5f9cf61..be4cbc8 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: spam.php,v 1.147 2007/05/04 14:55:36 henoheno Exp $
+// $Id: spam.php,v 1.148 2007/05/05 04:59:31 henoheno Exp $
 // Copyright (C) 2006-2007 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 //
@@ -37,6 +37,27 @@ function preg_grep_invert($pattern = '//', $input = array())
        }
 }
 
+// Remove redundant values from array()
+function array_unique_recursive($array = array())
+{
+       if (! is_array($array)) return $array;
+
+       $tmp = array();
+       foreach($array as $key => $value){
+               if (is_array($value)) {
+                       $array[$key] = array_unique_recursive($value);
+               } else {
+                       if (isset($tmp[$value])) {
+                               unset($array[$key]);
+                       } else {
+                               $tmp[$value] = TRUE;
+                       }
+               }
+       }
+       
+       return $array;
+}
+
 // Roughly strings(1) using PCRE
 // This function is useful to:
 //   * Reduce the size of data, from removing unprintable binary data
@@ -1143,11 +1164,9 @@ function check_uri_spam($target = '', $method = array())
                        if ($asap && $is_spam) break;
 
                        // Merge $blocked
-                       // TODO: unique the hosts
                        $blocked = array_merge_leaves($blocked, $_progress['blocked']);
 
                        // Merge $hosts
-                       // TODO: unique the hosts
                        $hosts   = array_merge_leaves($hosts,   $_progress['hosts']);
                }
 
@@ -1302,12 +1321,18 @@ function array_count_leaves($array = array(), $count_empty = FALSE)
 }
 
 // Merge two leaves' value
-function array_merge_leaves(& $array1, & $array2)
+function array_merge_leaves(& $array1, & $array2, $unique_values = TRUE)
 {
        // All NUMERIC keys are always renumbered from 0
-       return array_merge_recursive($array1, $array2);
+       $array = array_merge_recursive($array1, $array2);
+
+       // Redundant values (and keys) are vanished
+       if ($unique_values) $array = array_unique_recursive($array);
+
+       return $array;
 }
 
+
 // ---------------------
 // Reporting