Jump to content

Counting items in an array


grumpy

Recommended Posts

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

?>
Link to comment
https://forums.phpfreaks.com/topic/4497-counting-items-in-an-array/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.