From: henoheno Date: Sat, 5 May 2007 04:59:31 +0000 (+0900) Subject: array_unique_recursive() X-Git-Url: http://git.osdn.net/view?p=pukiwiki%2Fpukiwiki_sandbox.git;a=commitdiff_plain;h=91a697a9b00b89075f0914ead69ff13141c51142 array_unique_recursive() --- diff --git a/spam/SpamTest.php b/spam/SpamTest.php index df98e95..ec3b4e7 100644 --- a/spam/SpamTest.php +++ b/spam/SpamTest.php @@ -1,5 +1,5 @@ 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)); } diff --git a/spam/spam.php b/spam/spam.php index 5f9cf61..be4cbc8 100644 --- a/spam/spam.php +++ b/spam/spam.php @@ -1,5 +1,5 @@ $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