Jump to content


Photo

Complicated Counting


  • Please log in to reply
4 replies to this topic

#1 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 22 April 2006 - 01:53 PM

Hello,

I have a variable that looks like this:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]0lil alx9d1, BagFullOfRic, YOURMOM, fdndfnfd , fdndfnfd , fdndfnfd, fdndfnfd, fdndfnfd, fdndfnfd, sleeperpick10, BagFullOfRice, BagFullOfRice74, iix3JAD0REuU, iix3JAD0REuU, sinwillsin, sportzr4me18, sleeperpick10, BaSkEtBaLLQT8674, xpjisuuus, BagFullOfShyt, %n, %n, BagFullOfRice, Patelk0014, Patelk0014, BagFullOfRice, BagFullOfRice, , Patelk0014, flirtyazngrl09, bigshot9214, bigshot9214, bigshot9214, mnkmnkz16, bleausky22312, , , fashnFrEEk92, BagFullOfRice, BagFullOfRice, , xbsbdfbdbfdbfd, xbsbdfbdbfdbfd, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, , BagFullOfRice, %n, BagFullOfRice, %n, %n, , BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, %n, BagFullOfRice, BagFullOfRice, %n, BagFullOfRice, %n, BagFullOfRice, %n, BagFullOfShyt, BagFullOfShyt, BagFullOfShyt, rubberducky9314, rubberducky9314, rubberducky9314, %n, %n, BagFullOfRice, BagFullOfRice, BagFullOfRice, %n, Patelk0014, rubberducky9314, bigshot9214, bigshot9214, %n, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice[/quote]


Is it possible to count how many of the same names are in there?

For example:

(echoed out text)

0lil alx9d1 (1)
sleeperpick10 (2)
BagFullOfRice (34)
%n (27)
bigshot9214 (3)


Where the number in the parentheses is how many of the same names are in the variable.

Regards,

AIMMultimedia

#2 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 22 April 2006 - 03:41 PM

Yes, there is....

<?PHP
$var00 = "0lil alx9d1, BagFullOfRic, YOURMOM, fdndfnfd , fdndfnfd , fdndfnfd, fdndfnfd, fdndfnfd, fdndfnfd, sleeperpick10, BagFullOfRice,BagFullOfRice74, iix3JAD0REuU, iix3JAD0REuU, sinwillsin, sportzr4me18, sleeperpick10, BaSkEtBaLLQT8674, xpjisuuus,BagFullOfShyt, %n, %n, BagFullOfRice, Patelk0014, Patelk0014, BagFullOfRice, BagFullOfRice, , Patelk0014, flirtyazngrl09, bigshot9214, bigshot9214, bigshot9214, mnkmnkz16, bleausky22312, , , fashnFrEEk92, BagFullOfRice, BagFullOfRice, , xbsbdfbdbfdbfd, xbsbdfbdbfdbfd, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, , BagFullOfRice, %n, BagFullOfRice, %n, %n, , BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, %n, BagFullOfRice, BagFullOfRice, %n, BagFullOfRice, %n, BagFullOfRice, %n, BagFullOfShyt, BagFullOfShyt, BagFullOfShyt, rubberducky9314, rubberducky9314, rubberducky9314, %n, %n, BagFullOfRice, BagFullOfRice, BagFullOfRice, %n, Patelk0014, rubberducky9314, bigshot9214, bigshot9214, %n, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice";

###########
# convert the variable to an array

$var02 = explode(",",$var00);

?>
<pre>
<?PHP

print_r(array_count_values($var02));
?>
</pre>
<?PHP
 

?>

note that due to spaces in your variable it will appear that some of the elements are duplicated. to resolve this remove all spaces from the variable BEFORE you convert it to an array

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#3 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 22 April 2006 - 05:23 PM

Thanks!, but the results is this:


[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Array( [0lil alx9d1] => 1 [ BagFullOfRic] => 1 [ YOURMOM] => 1 [ fdndfnfd ] => 2 [ fdndfnfd] => 4 [ sleeperpick10] => 2 [ BagFullOfRice] => 42 [BagFullOfRice74] => 1 [ iix3JAD0REuU] => 2 [ sinwillsin] => 1 [ sportzr4me18] => 1 [ BaSkEtBaLLQT8674] => 1 [ xpjisuuus] => 1 [BagFullOfShyt] => 1 [ %n] => 13 [ Patelk0014] => 4 [ ] => 6 [ flirtyazngrl09] => 1 [ bigshot9214] => 5 [ mnkmnkz16] => 1 [ bleausky22312] => 1 [ fashnFrEEk92] => 1 [ xbsbdfbdbfdbfd] => 2 [ BagFullOfShyt] => 3 [ rubberducky9314] => 4).
[/quote]

Is there any way I can make it more neat, like


0lil alx9d1 (1) <br>
BagFullOfRic (1) <br>
YOURMOM] (1) <br>
fdndfnfd (2) <br>
fdndfnfd (4) <br>
sleeperpick10 (2) <br>
BagFullOfRice] (42) <br>
BagFullOfRice74] (1) <br>

Or at least:

iix3JAD0REuU] => 2
sinwillsin] => 1
sportzr4me18] => 1
BaSkEtBaLLQT8674] => 1
xpjisuuus] => 1
BagFullOfShyt] => 1



Thanks.

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 22 April 2006 - 09:38 PM

Surround the print_r() with the '<pre>' & '</pre>' tags:
<?php
$var02 = explode(",",$var00);
echo '<pre>' .  print_r(array_count_values($var02),true) . '</pre>';
?>

Or you can use a foreach
<?php
$var02 = explode(",",$var00);
foreach ($var02 as $k => $v)
    $var02[$k] = trim($v);
$var04 = array_count_values($var02);

foreach ($var04 as $k => $v)
    echo $k . ' (' . $v . ')<br>';

?>

Ken

#5 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 22 April 2006 - 09:57 PM

Thanks so much, it works perfectly.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users