0

Here is the small snippet to count the string occurrences of string without using string functions.

  <?php

$str ='1234567891222222222233333332';
$count = 0;
$ones='';
$twos='';
$threes='';

while(isset($str[$count])){
//echo $str[$count];

if($str[$count]== 1)
$ones += count($str[$count]);

if($str[$count]== 2)
$twos += count($str[$count]);

if($str[$count]== 3)
$threes += count($str[$count]);

++$count;
}
echo 'total number of 1\'s = '.$ones.'<br/>';
echo 'total number of 2\'s = '.$twos.'<br/>';
echo 'total number of 3\'s = '.$threes.'<br/>';
?>

Please can anyone shorter the code in efficient way...

0

5 Answers 5

2

You can do this. I'm not sure why you're using count(), as you can just increment it.

$count = 0;
$countSizes = array();

while(isset($str[$count++])) {
    $countSizes[ $str[$count] ]++;
}

$countSizes will now have the count of each number in the string respective to its index.

Sign up to request clarification or add additional context in comments.

1 Comment

Be careful not to increment $count in the while condition, since this will cause your array index to not line up with the actual results. This should read: while(isset($str[$count])) { $countSizes[ $str[$count++] ]++; } instead
2

You can use array_count_values and str_split if your numbers range 0-9

$result = array_count_values(str_split($str));

Output

var_dump($result);

array (size=9)
  1 => int 2
  2 => int 12
  3 => int 8
  4 => int 1
  5 => int 1
  6 => int 1
  7 => int 1
  8 => int 1
  9 => int 1

Comments

0
for($i=1;$i<=3;$i++){
    preg_match_all ( $strn , $i, $matches);
    //preg_match_all stores the found values in $matches, you can count them easily...
    echo 'There is '.count($matches).' "'.$i.'".';
}

Comments

0

sounds like homework to me:

$counters = array_fill(0,10,0);
$count = 0;
while(isset($str[$count])){
    $counters[$str[$count++]]++;
}
foreach($counters as $key => $value) {
    echo "Total number of {$key}s = {$value}", PHP_EOL;
}

or even using

array_count_values(str_split($str));

if str_split() is permitted

Comments

0

Use:

<?php
$str ='1234567891222222222233333332';

$i=0;
$char1 = $str[$i];
$isExists = array();
$found_array = array();
while(!empty($char1)) {
    if(in_array($char1, $isExists)) { 
        $i++;
        $char1 = isset($str[$i]) ? $str[$i] : '';
        continue; 
    };
    $count = 0;
    $j=0;
    $char2 = $str[$j];
    while(!empty($char2)) {
        //echo "$char1 = $char2<br>";
        if($char1==$char2) $count++;
        $j++;
        $char2 = isset($str[$j]) ? $str[$j] : '';
    }
    $isExists[] = $char1;
    $found_array[$char1] = $count;
    $i++;
    $char1 = isset($str[$i]) ? $str[$i] : '';
}

foreach($found_array as $char=>$count) {
    echo "total number of $char's = $count <br/>";  
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.