PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
sebamed
Forum Newbie
Posts: 4 Joined: Mon Jul 08, 2002 9:23 am
Post
by sebamed » Sat Aug 03, 2002 5:17 am
I thought something like this, on the first run it didn't work, as I wanted. So if anyone could the check even if the idea is right I’d appreciate it
Code: Select all
while(list($key, $val) =each($array) {
$count = $count_elementsї$val]++;
}
hob_goblin
Forum Regular
Posts: 978 Joined: Sun Apr 28, 2002 9:53 pm
Contact:
Post
by hob_goblin » Sat Aug 03, 2002 8:23 am
Code: Select all
$array = array("foo", "foo", "bar");
$count = 0;
for($i = 0; $i < count($array); $i++){
$tmp = $arrayї$i];
unset($arrayї$i]);
if(in_array($tmp, $array)){
$count++;
}
$arrayї$i] = $tmp;
}
try that
hob_goblin
Forum Regular
Posts: 978 Joined: Sun Apr 28, 2002 9:53 pm
Contact:
Post
by hob_goblin » Sat Aug 03, 2002 8:34 am
btw, on the previous example, $count should be 2.. ($count will be the number of duplicates... so if i had an array with foo,foo,foo,bar ... it would be 3...)
daemorhedron
Forum Commoner
Posts: 52 Joined: Tue Jul 23, 2002 11:03 am
Post
by daemorhedron » Sat Aug 03, 2002 1:01 pm
Nice code, but what about simply using the php function array_count_values() ?
HTH!
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sat Aug 03, 2002 3:20 pm
or
Code: Select all
$c = count(array_flip($array)) // number of non-duplicate-values