grumpy Posted March 9, 2006 Share Posted March 9, 2006 Hello-I'm trying to display the count of how many of a particular character appear in an array. I have an array named 'most' (which comes from a form), which contains a string of 20 characters, some of which are repeated. I'm trying to find and display how many "B"s are in the array, how many "T"s, etc. But I can't get it to work. Here is my code. Any help would be appreciated. Thanks.<?include ('connect.php');$most = urlencode($most);echo $most; // just to test$most = explode(" ", $most);$B = 0;$T = 0;$S = 0;$N = 0;$Z = 0;foreach($most as $value){switch($value){case 'B':$B++;break;case 'T':$T++;break;case 'S':$S++;break;case 'N':$N++;break;break;case 'Z':$Z++;break;}}?> Quote Link to comment Share on other sites More sharing options...
Hooker Posted March 9, 2006 Share Posted March 9, 2006 You have a double break here:[code]$N++;break;break;[/code]although i beleive it'd work better if you removed the break, as it finds the first letter your looking for i beleive it ends the loop with "break;"PHP Manual:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. [/quote]so try something like:[code]<?include ('connect.php');$most = urlencode($most);echo $most; // just to test$most = explode(" ", $most);$B = 0;$T = 0;$S = 0;$N = 0;$Z = 0;foreach($most as $value){switch($value){case 'B':$B++;case 'T':$T++;case 'S':$S++;case 'N':$N++;case 'Z':$Z++;}}?>[/code] Quote Link to comment Share on other sites More sharing options...
grumpy Posted March 9, 2006 Author Share Posted March 9, 2006 Hi-Thanks for you answer. Unfortunately, your code also doesn't work. Still no results. I've been working on this all day and I'm getting very frustrated. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 9, 2006 Share Posted March 9, 2006 A few questions on your code. Look for the comments I've added below:[code]<?phpinclude ('connect.php');$most = urlencode($most); // why are you doing a urlencode?echo $most; // just to test$most = explode(" ", $most); // what does the string coming in look like? It almost looks as though you expect spaces between each character.$B = 0;$T = 0;$S = 0;$N = 0;$Z = 0;foreach($most as $value) // Please indent your code... { switch($value) { case 'B': $B++; break; case 'T': $T++; break;case 'S':$S++;break;case 'N':$N++;break;break;case 'Z':$Z++;break;}}?>[/code]Now for a possible solution:[code]<?php $str = ';lkdjg kljklJ;J;;FJGKLJEGJREKJG LKJSKDKLKLDJGJGKLJ'; $tmp = array(); for ($i=0;$i<strlen($str);$i++) { if (array_key_exists($str{$i},$tmp)) $tmp[$str{$i}]++; else $tmp[$str{$i}] = 1; } ksort($tmp); echo '<pre>' . print_r($tmp,true) . '</pre>';?>[/code]Adapt the above code to your needs.Ken Quote Link to comment Share on other sites More sharing options...
Barand Posted March 9, 2006 Share Posted March 9, 2006 or[code]$str = 'BBT NNSXTZBST XSNZ ZST';$res = array ( 'B' => 0, 'N' => 0, 'S' => 0, 'T' => 0, 'Z' => 0);$l = strlen($str);for ($i=0; $i < $l; $i++) { $c = $str{$i}; if (isset($res[$c])) $res[$c]++;}foreach ($res as $c => $num) echo "<br>$c : $num";[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.