note 69627 added to function.array-unique
| From: | pulsarkowy at NOSPAM dot gmail dot com | Date: | Wed, 13 Sep 2006 11:30:55 +0000 |
| Subject: | note 69627 added to function.array-unique | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-117094@lists.php.net to get a copy of this message | ||
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:
$myarray=my_array_unique($myarray)
in return it produces an 3 fields array:
1st is index number, 2nd is the value, 3rd is counter.
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):
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;
}
----
Server IP: 195.136.184.34
Probable Submitter: 81.219.203.11
----
Manual Page -- http://www.php.net/manual/en/function.array-unique.php
Edit -- https://master.php.net/note/edit/69627
Del: integrated -- https://master.php.net/note/delete/69627/integrated
Del: useless -- https://master.php.net/note/delete/69627/useless
Del: bad code -- https://master.php.net/note/delete/69627/bad+code
Del: spam -- https://master.php.net/note/delete/69627/spam
Del: non-english -- https://master.php.net/note/delete/69627/non-english
Del: in docs -- https://master.php.net/note/delete/69627/in+docs
Del: other reasons-- https://master.php.net/note/delete/69627
Reject -- https://master.php.net/note/reject/69627
Search -- https://master.php.net/manage/user-notes.php