Jump to content


Photo

Counting items in an array


  • Please log in to reply
4 replies to this topic

#1 grumpy

grumpy
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 09 March 2006 - 03:07 AM

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;
}
}

?>

#2 Hooker

Hooker
  • Members
  • PipPipPip
  • Advanced Member
  • 193 posts
  • LocationWales, UK

Posted 09 March 2006 - 03:27 AM

You have a double break here:

$N++;
break;
break;

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:

<?
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++;
}
}

?>


#3 grumpy

grumpy
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 09 March 2006 - 03:43 AM

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.

#4 kenrbnsn

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

Posted 09 March 2006 - 05:00 AM

A few questions on your code. Look for the comments I've added below:
<?php
include ('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;
}
}
?>

Now for a possible solution:
<?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>';
?>

Adapt the above code to your needs.

Ken

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 09 March 2006 - 07:45 AM

or

$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";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users