note 68339 added to function.array-unique
| From: | adotfotoglidisat7mbitdotnet at osu1 dot php dot net | Date: | Mon, 24 Jul 2006 03:25:24 +0000 |
| Subject: | note 68339 added to function.array-unique | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115068@lists.php.net to get a copy of this message | ||
In addition to brettz9's remove_dups:
This one will actually take a multi-dimensional array and return it minus the duplicates in regard
to the specified element of the inner arrays.
<?php
/*
Initialising new array to the first element of the given array.
Check whether current element in initial array has already been added to new array.
If yes break to save us some time. If no, then add current element to new array.
*/
function remove_dups($array, $row_element) {
$new_array[0] = $array[0];
foreach ($array as $current) {
$add_flag = 1;
foreach ($new_array as $tmp) {
if ($current[$row_element]==$tmp[$row_element]) {
$add_flag = 0; break;
}
}
if ($add_flag) $new_array[] = $current;
}
return $new_array;
} // end function remove_dups
?>
______________________________________________
EXAMPLE
<?php
$array[0] = array('Charles','Wade',233);
$array[1] = array('Charles','Watson',234);
$array[2] = array('Tom','Wade',235);
$array = remove_dups($array,0);
?>
The above example will output:
Array
(
[0] => Array ([0] => 'Charles' [1] => 'Wade' [2] => 233)
[1] => Array ([0] => 'Tom' [1] => 'Wade' [2] => 235)
)
<?php
remove dups($array,1);
?>
will output:
Array
(
[0] => Array ([0] => 'Charles' [1] => 'Wade' [2] => 233)
[1] => Array ([1] => 'Charles' [1] => 'Watson' [2] => 234)
)
At each case it removes all inner arrays that have a duplicate in the specified position and keeps
the first inner array only.
----
Server IP: 83.138.144.80
Probable Submitter: 80.175.158.55
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.array-unique.php
Edit -- http://master.php.net/note/edit/68339
Del: integrated -- http://master.php.net/note/delete/68339/integrated
Del: useless -- http://master.php.net/note/delete/68339/useless
Del: bad code -- http://master.php.net/note/delete/68339/bad+code
Del: spam -- http://master.php.net/note/delete/68339/spam
Del: non-english -- http://master.php.net/note/delete/68339/non-english
Del: in docs -- http://master.php.net/note/delete/68339/in+docs
Del: other reasons-- http://master.php.net/note/delete/68339
Reject -- http://master.php.net/note/reject/68339
Search -- http://master.php.net/manage/user-notes.php