so .... my problem was multidimensional sort.
<?php
$new = array();
$exclude = array("");
for ($i = 0; $i<=count($attribs)-1; $i++) {
if (!in_array(trim($attribs[$i]["price"]) ,$exclude)) { $new[] = $attribs[$i]; $exclude[] = trim($attribs[$i]["price"]); }
}
?>
Array $attribs is an array contaning arrays. Each array in the $attrib array consists in multiple fields (ex: name, lenght, price, etc.) to be more simpler in speech think that $attrib is the array resulted by a search sql query done by a visitator on your online shoopping website ... (so ... each array in the $attrib is a product :P) if you want to sort only the uniq results use the above or use this:
<?php
/* Our Array of products */
$attribs[] = array(
"name" => "Test Product 1",
"length" => "42 cm",
"weight" => "0,5 kg",
"price" => "10 $",
"stock" => "100",
);
$attribs[] = array(
"name" => "Test Product 2",
"length" => "42 cm",
"weight" => "1,5 kg",
"price" => "10 $",
"stock" => "200",
);
/* The nice stuff */
$new = array();
$exclude = array("");
for ($i = 0; $i<=count($attribs)-1; $i++) {
if (!in_array(trim($attribs[$i]["price"]) ,$exclude)) { $new[] = $attribs[$i]; $exclude[] = trim($attribs[$i]["price"]); }
}
print_r($new); // $new is our sorted array
?>
Have fun tweaking this ;)) i know you will ;))
From Romania With Love
array_unique
(PHP 4 >= 4.0.1, PHP 5)
array_unique — Removes duplicate values from an array
Description
Takes an input array and returns a new array without duplicate values.
Note that keys are preserved. array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.
Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used.
Parameters
- array
-
The input array.
Return Values
Returns the filtered array.
Examples
Example #1 array_unique() example
<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>
The above example will output:
Array ( [a] => green [0] => red [1] => blue )
Example #2 array_unique() and types
<?php
$input = array(4, "4", "3", 4, 3, "3");
$result = array_unique($input);
var_dump($result);
?>
The above example will output:
array(2) { [0] => int(4) [2] => string(1) "3" }
array_unique
06-Nov-2008 11:23
08-Oct-2008 10:29
I came across one limitation of array_unique: it doesn't work properly if you have arrays inside your main array.
The reason is that to compare two values, the function tests if (string) $value1 == (string) $value2. So if $value1 and $value2 are both arrays, the function will evaluate the test to 'Array' == 'Array', and decide that the $values are repeated even if the arrays are different.
So a work around is to find a better conversion of an array to a string, which can be done with json:
<?php
print "define an array with repeated scalar '1' and repeated 'array(1)':";
$a_not_unique = array(
'a' => 1,
'b' => 1,
'c' => 2,
'd' => array(1),
'e' => array(1),
'f' => array(2),
);
print_r($a_not_unique);
print "try to use simply array_unique, which will not work since it exludes 'array(2)':";
$a_unique_wrong = array_unique($a_not_unique);
print_r($a_unique_wrong);
print "convert to json before applying array_unique, and convert back to array, which will successfully keep 'array(2)':";
$a_unique_right = $a_not_unique;
array_walk($a_unique_right, create_function('&$value,$key', '$value = json_encode($value);'));
$a_unique_right = array_unique($a_unique_right);
array_walk($a_unique_right, create_function('&$value,$key', '$value = json_decode($value, true);'));
print_r($a_unique_right);
?>
Results:
define an array with repeated scalar '1' and repeated 'array(1)':
Array
(
[a] => 1
[b] => 1
[c] => 2
[d] => Array
(
[0] => 1
)
[e] => Array
(
[0] => 1
)
[f] => Array
(
[0] => 2
)
)
try to use simply array_unique, which will not work since it exludes 'array(2)':
Array
(
[a] => 1
[c] => 2
[d] => Array
(
[0] => 1
)
)
convert to json before applying array_unique, and convert back to array, which will successfully keep 'array(2)':
Array
(
[a] => 1
[c] => 2
[d] => Array
(
[0] => 1
)
[f] => Array
(
[0] => 2
)
)
26-Aug-2008 07:30
another method to get unique values is :
$alpha=array('a','b','c','a','b','d','e','f','f');
$alpha= array_keys(array_count_values($alpha));
print_r($alpha);
Output:
Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f )
14-Aug-2008 11:39
Here's another solution for returning an array that only includes repeated values. There is one given below but it only works on numerically indexed arrays; this one is more comprehensive since I used the foreach iterator. Also, this one preserves keys--in that the returned result contains a distinct list of repeats (storing only the first instance it encounters of each duplicate value).
<?php
function array_repeated($array)
{
if ( !is_array($array) ) {
return false;
}
$duplicates = array();
foreach ( $array as $key => $val ) {
end($array);
$k = key($array);
$v = current($array);
while ( $k !== $key ) {
if ( $v === $val ) {
$duplicates[$key] = $v;
break;
}
$v = prev($array);
$k = key($array);
}
}
return $duplicates;
}
?>
28-Jul-2008 06:47
I had a problem with array_unique and multidimensional arrays ... Maybe there's a better way to do this, but this will work for any dimensional arrays.
<?php
function arrayUnique($myArray)
{
if(!is_array($myArray))
return $myArray;
foreach ($myArray as &$myvalue){
$myvalue=serialize($myvalue);
}
$myArray=array_unique($myArray);
foreach ($myArray as &$myvalue){
$myvalue=unserialize($myvalue);
}
return $myArray;
}
?>
19-Jun-2008 10:41
a lot of people create functions just to fix a notice error about an undefined index and removes blank array value.
why not using foreach instead of the for loop?
example:
<?php
foreach ($arrayname as $key => $value) {
//do what you want with $value withaout index stress
}
?>
14-Apr-2008 02:34
case insensitive for PHP v4.x and up.
<?php
function in_iarray($str, $a){
foreach($a as $v){
if(strcasecmp($str, $v)==0){return true;}
}
return false;
}
function array_iunique($a){
$n = array();
foreach($a as $k=>$v){
if(!in_iarray($v, $n)){$n[$k]=$v;}
}
return $n;
}
$input = array("aAa","bBb","cCc","AaA","ccC","ccc","CCC","bBB","AAA","XXX");
$result = array_iunique($input);
print_r($result);
/*
Array
(
[0] => aAa
[1] => bBb
[2] => cCc
[9] => XXX
)
*/
?>
02-Mar-2008 04:46
I needed to identify email addresses in a data table that were replicated, so I wrote the array_not_unique() function:
<?php
function array_not_unique($raw_array) {
$dupes = array();
natcasesort($raw_array);
reset ($raw_array);
$old_key = NULL;
$old_value = NULL;
foreach ($raw_array as $key => $value) {
if ($value === NULL) { continue; }
if ($old_value == $value) {
$dupes[$old_key] = $old_value;
$dupes[$key] = $value;
}
$old_value = $value;
$old_key = $key;
}
return $dupes;
}
$raw_array = array();
$raw_array[1] = 'abc@xyz.com';
$raw_array[2] = 'def@xyz.com';
$raw_array[3] = 'ghi@xyz.com';
$raw_array[4] = 'abc@xyz.com'; // Duplicate
$common_stuff = array_not_unique($raw_array);
var_dump($common_stuff);
?>
19-Dec-2007 08:20
To clarify the note above Example #1, the function appears to cast the elements to strings for comparison and then return the type of the first unique element encountered.
<?php
$input = array(0, 2, "2", 3, "9", 9);
$result = array_unique($input);
var_dump($result);
?>
array(4) {
[0]=>
int(0)
[1]=>
int(2)
[3]=>
int(3)
[4]=>
string(1) "9"
}
28-Oct-2007 11:18
case insensitive
<?php
function array_iunique($array) {
return array_intersect_key($array,array_unique(
array_map(strtolower,$array)));
}
?>
11-Oct-2007 09:58
<?php
function array_unique_FULL($array) {
foreach($array as $k => $v) {
if (is_array($v)) {
$ret = array_unique_FULL(array_merge($ret, $v));
} else {
$ret[$k] = $v;
}
} //for
return array_unique($ret);
}
?>
Array
(
[0] => js/pt.js
[1] => js/selectfile.js
[2] => js/pt.js
[3] => js/proyecto.js
[4] => Array
(
[0] => Array
(
[0] => js/selectfile.js
[1] => js/selectfile.js
[2] => js/y otro mas.js
)
[1] => q se yo
)
)
go array_unique_FULL()!!!!
Array
(
[0] => js/pt.js
[1] => js/selectfile.js
[3] => js/proyecto.js
[6] => js/y otro mas.js
[5] => q se yo
)
02-Oct-2007 09:05
Smaller version for PHP5
<?php
# array array_unique_save (array array [, bool preserve_keys] )
function array_unique_save ($a, $pk = true) {
$a = array_diff_key($a, array_unique($a));
return ($pk ? $a : array_values($a));
}
?>
17-Sep-2007 01:57
Should have thought of this sooner. x.x
<?php
function array_unique_save_php4 ($a, $pk = true) {
$t = array_keys(array_unique($a));
foreach ($t as $k) { unset($a[$k]); }
return ($pk ? $a : array_values($a));
}
?>
11-Sep-2007 08:50
This is a solution to remove duplicate values from an array
<?php
$array[0] = "Yellow";
$array[1] = "Green";
$array[2] = "Yellow";
$array[3] = "Blue";
$array[4] = "Yellow";
$array = array_keys(array_flip($array));
//$array will output Yellow Green Blue
?>
22-Aug-2007 01:20
Suggestion for being able to use array_unique on array of arrays/objects:
<?php
foreach ($arrayOfArrays as $key=>$value) {
$arrayOfArrays[$key] = "'" . serialize($value) . "'";
}
$arrayOfArrays = array_unique($arrayOfArrays);
foreach ($arrayOfArrays as $key=>$value) {
$arrayOfArrays[$key] = unserialize(trim($value, "'"));
}
?>
30-Jul-2007 01:08
Another way to 'unique column' an array, in this case an array of objects:
Keep the desired unique column values in a static array inside the callback function for array_filter.
Example:
<?php
/* example object */
class myObj {
public $id;
public $value;
function __construct( $id, $value ) {
$this->id = $id;
$this->value = $value;
}
}
/* callback function */
function uniquecol( $obj ) {
static $idlist = array();
if ( in_array( $obj->id, $idlist ) )
return false;
$idlist[] = $obj->id;
return true;
}
/* a couple of arrays with second array having an element with same id as the first */
$list = array( new myObj( 1, 1 ), new myObj( 2, 100 ) );
$list2 = array( new myObj( 1, 10 ), new myObj( 3, 100 ) );
$list3 = array_merge( $list, $list2 );
$unique = array_filter( $list3, 'uniquecol' );
print_r( $list3 );
print_r( $unique );
?>
In addition, use array_merge( $unique ) to reindex.
11-Jul-2007 06:08
Another way to get unique objects/arrays:
<?php
function my_array_unique($array) {
return array_intersect_key($array, array_unique(array_map('serialize', $array)));
}
?>
23-May-2007 01:22
This one would work with objects and arrays also.
<?php
function my_array_unique($array, $keep_key_assoc = false)
{
$duplicate_keys = array();
$tmp = array();
foreach ($array as $key=>$val)
{
// convert objects to arrays, in_array() does not support objects
if (is_object($val))
$val = (array)$val;
if (!in_array($val, $tmp))
$tmp[] = $val;
else
$duplicate_keys[] = $key;
}
foreach ($duplicate_keys as $key)
unset($array[$key]);
return $keep_key_assoc ? $array : array_values($array);
}
?>
13-Apr-2007 03:44
An updated version of webcreators script that fixes a notice error about an undefined index and removes blank array value.
<?php
function my_array_unique($from) {
for ($i=count($from)-1;$i>1;$i--) {
$last = $from[$i];
$from[$i] = false;
if (!in_array($last,$from)) {
$from[$i]=$last;
}
}
$from = array_unique($from);
$from = array_slice($from,0,count($from)-1);
return $from;
}
?>
13-Dec-2006 11:45
What if I only want the duplicated items, not the unique ones?
I tried to do it by using array_diff of an array and its unique, but it did not work (any help?); so I made this function with a relatively good efficiency:
<?php
function array_repeated($array) {
if(!is_array($array)) return false;
$repeated_values = Array();
$array_unique = array_unique($array);
if(count($array)-count($array_unique)) {
for($i=0;$i<count($array);$i++) {
if(!array_key_exists($i, $array_unique)) $repeated_values[] = $array[$i];
}
}
return $repeated_values;
}
?>
This function is based on the behaviour of the array_unique function that mantains the indexes of the array when deleting the item.
13-Dec-2006 11:26
Note that array_unique preserves the keys, even if the array is not associative:
$values = Array("john@hotmail.com", "peter@hotmail.com", "will@hotmail.com", "john@hotmail.com", "laura@hotmail.com", "mariah@hotmail.com");
echo "<br>".count($values)." values.<br>";
var_dump($values);
$unique_values = array_unique($values);
echo "<br>".count($unique_values)." unique values.<br>";
var_dump($unique_values);
RESULT:
6 values.
array(6) {
[0]=>
string(16) "john@hotmail.com"
[1]=>
string(17) "peter@hotmail.com"
[2]=>
string(16) "will@hotmail.com"
[3]=>
string(16) "john@hotmail.com"
[4]=>
string(17) "laura@hotmail.com"
[5]=>
string(18) "mariah@hotmail.com"
}
5 unique values.
array(5) {
[0]=>
string(16) "john@hotmail.com"
[1]=>
string(17) "peter@hotmail.com"
[2]=>
string(16) "will@hotmail.com"
[4]=>
string(17) "laura@hotmail.com"
[5]=>
string(18) "mariah@hotmail.com"
}
$unique_values[3] is missing. Use array_merge or array_values if you need the index to be linear.
01-Nov-2006 05:59
Just to note that array_unique, treats null values as none unique values. So if your using array_unique to detect duplicate values it will also detect multiple null values.
27-Oct-2006 09:16
For people looking at the flip flip method for getting unique values in a simple array. This is the absolute fastest method:
<?php
$unique = array_keys(array_flip($array));
?>
It's marginally faster as:
<?php
$unique = array_merge(array_flip(array_flip($array)));
?>
And it's marginally slower as:
<?php
$unique array_flip(array_flip($array)); // leaves gaps
?>
It's still about twice as fast or fast as array_unique.
This tested on several different machines with 100000 random arrays. All machines used a version of PHP5.
09-Oct-2006 07:27
Here's the shortest line of code I could find/create to remove all duplicate entries from an array and then reindex the keys.
<?php
// Fruits, vegetables, and other food:
$var = array('apple','banana','carrot','cat','dog','egg','eggplant','fish');
$var = array_values(array_unique($var));
?>
30-Sep-2006 04:01
Taking the advantage of array_unique, here is a simple function to check if an array has duplicate values.
It simply compares the number of elements between the original array and the array_uniqued array.
<?php
function array_search_dups($array)
{
$dup_array = $array;
$dup_array = array_unique($dup_array);
if(count($dup_array) != count($array))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
13-Sep-2006 01:30
This function below will remove multiple values from array, remove 'empty' fields an also count how many times the given value occured.
it's rather slow and not 'elegant' but works.
usage:
<?php
$myarray = my_array_unique($myarray);
?>
in return it produces an 3 fields array:
1st is index number, 2nd is the value, 3rd is counter.
<?php
function my_array_unique($tablica)
{
$tnum=count($tablica);
$i=1;
$k=1;
$t[1]['product']="";
$t[1]['count']=1;
while($i<=$tnum) {
if (!array_multi_search($tablica[$i], $t)) {
$t[$k]['product']=$tablica[$i];
$t[$k]['count']=1;
$k++;
}
else {
$y=1;
while ($y<=count($t)) {
if ($t[$y]['product']==$tablica[$i])
$t[$y]['count']++;
$y++;
}
}
$i++;
}
$tablica=$t;
return $tablica;
}
?>
the function uses another function that i've found on the php.net site (i'm posting it only for informational reasons - i can't remember who wrote it):
<?php
function array_multi_search( $p_needle, $p_haystack )
{
if( !is_array( $p_haystack ) )
{
return false;
}
if( in_array( $p_needle, $p_haystack ) )
{
return true;
}
foreach( $p_haystack as $row )
{
if( array_multi_search( $p_needle, $row ) )
{
return true;
}
}
return false;
}
?>
[EDIT BY danbrown AT php DOT net: The array_multi_search() function was originally written by 'czeslaw' and posted to the in_array() function manual entry on 18 February, 2006.]
24-Jul-2006 05:25
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.
15-Jul-2006 09:14
<?php
//Fn for array_unique column-wise for multi-dimensioanl array without losing keys | Start
function array_uniquecolumn($arr)
{
$rows = sizeof($arr);
$columns = sizeof($arr[0]);
$columnkeys = array_keys($arr[0]);
for($i=0; $i<$columns; $i++)
{
for($j=0;$j<$rows;$j++)
{
for($k = $j+1; $k<$rows; $k++)
{
if($arr[$j][$columnkeys[$i]] == $arr[$k][$columnkeys[$i]])
$arr[$k][$columnkeys[$i]] = "";
}
}
}
return ($arr);
}
//Fn for array_unique column-wise for multi-dimensioanl array without losing keys | Stop
$arrUGCourse[]= array( "CTR" => "1",
"UGCOURSE"=>"ABC",
"TSINITIATE"=>"540",
"COUNT"=>"34",
"ENTRY_DT"=>"2006-05-01",
"CUMULATIVE"=> 44);
$arrUGCourse[]= array( "CTR" => "2",
"UGCOURSE"=>"ABC",
"TSINITIATE"=>"5401",
"COUNT"=>"341",
"ENTRY_DT"=>"2006-05-11",
"CUMULATIVE"=> 44);
print_r(array_uniquecolumn($arrUGCourse));
?>
20-Apr-2006 12:52
I used the code submitted by "agarcia" regarding multi-dimensional arrays but keys with no data were still there. I've just added a line to completely unset the redondant part of the array so it is now acting like array_unique.
<?php
function remove_dup($matriz) {
$aux_ini=array();
$entrega=array();
for($n=0;$n<count($matriz);$n++) {
$aux_ini[]=serialize($matriz[$n]);
}
$mat=array_unique($aux_ini);
for($n=0;$n<count($matriz);$n++) {
$entrega[]=unserialize($mat[$n]);
}
foreach ($entrega as $key => $row){
if (!is_array($row)) { unset($entrega[$key]); }
}
return $entrega;
}
?>
Thanks!
Davin Baragiotta
14-Apr-2006 06:02
The shortest way i found to remove duplicate array from a column,
For example if you parse Multiple XML sources, you can remove duplicate items that contain the same link.
<?PHP
function remove_duplicate($array, $field)
{
foreach ($array as $sub)
$cmp[] = $sub[$field];
$unique = array_unique($cmp);
foreach ($unique as $k => $rien)
$new[] = $array[$k];
return $new;
}
?>
31-Mar-2006 10:41
This is a script for multi_dimensional arrays
<?php
function remove_dup($matriz) {
$aux_ini=array();
$entrega=array();
for($n=0;$n<count($matriz);$n++)
{
$aux_ini[]=serialize($matriz[$n]);
}
$mat=array_unique($aux_ini);
for($n=0;$n<count($matriz);$n++)
{
$entrega[]=unserialize($mat[$n]);
}
return $entrega;
}
?>
09-Mar-2006 09:51
If you have a multi-dimensional array and wish to remove duplicates from a particular "column" as well as the corresponding values in the other "columns", you might find the following helpful.
<?php
function remove_dups($array, $index) {
$array_count = count($array);
$array_count_inner = count($array[$index]);
for ($i=0; $i<$array_count_inner; $i++) {
for ($j=$i+1; $j<$array_count_inner; $j++) {
if ($array[$index][$i]==$array[$index][$j]) {
for ($k=0; $k<$array_count; $k++) {
unset($array[$k][$i]);
} // end for
} // end if
} // end for
} // end for
return $array;
} // end function remove_dups
?>
27-Jan-2006 02:18
This is a recursive arrayUnique function for arrays of any dimension. (tested with 4-dimensional array)
The line '$newArray=deleteEmpty($newArray);' is optional and removes empty keys and values
<?php
function arrayUnique($myArray)
{
$newArray = Array();
if (is_array($myArray))
{
foreach($myArray as $key=>$val)
{
if (is_array($val))
{
$val2 = arrayUnique($val);
}
else
{
$val2 = $val;
$newArray=array_unique($myArray);
$newArray=deleteEmpty($newArray);
break;
}
if (!empty($val2))
{
$newArray[$key] = $val2;
}
}
}
return ($newArray);
}
function deleteEmpty($myArray)
{
$retArray= Array();
foreach($myArray as $key=>$val)
{
if (($key<>"") && ($val<>""))
{
$retArray[$key] = $val;
}
}
return $retArray;
}
?>
03-Jan-2006 07:47
Problem:
I have loaded an array with the results of a database
query. The Fields are 'FirstName' and 'LastName'.
I would like to find a way to contactenate the two
fields, and then return only unique values for the
array. For example, if the database query returns
three instances of a record with the FirstName John
and the LastName Smith in two distinct fields, I would
like to build a new array that would contain all the
original fields, but with John Smith in it only once.
Thanks for: Colin Campbell
Solution:
<?php
/**
* The same thing than implode function, but return the keys so
*
* <code>
* $_GET = array('id' => '4587','with' => 'key');
* ...
* echo shared::implode_with_key('&',$_GET,'='); // Resultado: id=4587&with=key
* ...
* </code>
*
* @param string $glue Oque colocar entre as chave => valor
* @param array $pieces Valores
* @param string $hifen Separar chave da array do valor
* @return string
* @author memandeemail at gmail dot com
*/
function implode_with_key($glue = null, $pieces, $hifen = ',') {
$return = null;
foreach ($pieces as $tk => $tv) $return .= $glue.$tk.$hifen.$tv;
return substr($return,1);
}
/**
* Return unique values from a tree of values
*
* @param array $array_tree
* @return array
* @author memandeemail at gmail dot com
*/
function array_unique_tree($array_tree) {
$will_return = array(); $vtemp = array();
foreach ($array_tree as $tkey => $tvalue) $vtemp[$tkey] = implode_with_key('&',$tvalue,'=');
foreach (array_keys(array_unique($vtemp)) as $tvalue) $will_return[$tvalue] = $array_tree[$tvalue];
return $will_return;
}
$problem = array_fill(0,3,
array('FirstName' => 'John', 'LastName' => 'Smith')
);
$problem[] = array('FirstName' => 'Davi', 'LastName' => 'S. Mesquita');
$problem[] = array('FirstName' => 'John', 'LastName' => 'Tom');
print_r($problem);
print_r(array_unique_tree($problem));
?>
28-Oct-2005 01:25
array_unique function starts its comparation from beginning and pop the key off if there is more values inside array. The last one remains. But i needed to hold priority of the order of values and let the first one in.
Here is my easy solution:
<?php
function my_array_unique($from)
{
for ($i=count($from);$i>1;$i--)
{
$last = $from[$i];
$from[$i] = "";
if (!in_array($last,$from))
$from[$i]=$last;
}
return array_unique($from);
}
# One empty value remains in array.
# But its very easy to separate it while using output array.
?>
27-Sep-2005 06:09
Yet another Array_Unique for multi-demensioned arrays. I've only tested this on two-demensioned arrays, but it could probably be generalized for more, or made to use recursion.
This function uses the serialize, array_unique, and unserialize functions to do the work.
<?php
function multi_unique($array) {
foreach ($array as $k=>$na)
$new[$k] = serialize($na);
$uniq = array_unique($new);
foreach($uniq as $k=>$ser)
$new1[$k] = unserialize($ser);
return ($new1);
}
?>
21-Sep-2005 04:20
array_unique for multidimensional arrays. similar to the DISTINCT in SQL function.
the function can group, sum and count keys
<?PHP
/*
$array - nothing to say
$group_keys - columns which have to be grouped - can be STRING or ARRAY (STRING, STRING[, ...])
$sum_keys - columns which have to be summed - can be STRING or ARRAY (STRING, STRING[, ...])
$count_key - must be STRING - count the grouped keys
*/
function array_distinct 