Php how many three/four/five digit numbers do I have in an array

Issue

I have an array which has several numbers.
What I want to do is get a loop and run into this array and count how many two-digits numbers, three-digits, four-digits numbers do I have.

I also tried this:

$threeDigits=0;
$fourDigits=0;
$fiveDigits=0;
$sixDigits=0;

$myArray=(123,1234,12345,123456,1234567,111,222)
for($i=1,$size=count($myArray);$i<=$size;i++)
{
if(strlen($==3)) $threeDigits++;
else if(strlen($i==4)) $fourDigits++;
else if(strlen($i==5)) $fiveDigits++;
else if(strlen($i==6)) $sixDigits++;
}
echo "There are " .$fourDigits. " numbers with 4 digits.";
echo "There are " .$threeDigits. " numbers with 3 digits.";
echo "There are " .$fiveDigits. " numbers with 5 digits.";
echo "There are " .$sixDigits. " numbers with 6 digits.";

But somehow it only reads them as one. As you can see, in my array there are three three-digit numbers but when I print it out it says I have only one. What do you think it might be the problem here?

Solution

Since I couldn’t find any answer that "suited" my code better, I tried and solved this out.

Here’s my code if someone else in the future needs it.

<?php
$threeDigits=0;
$fourDigits=0;
$fiveDigits=0;
$sixDigits=0;

$myArray=array(123,111,1234,12345,123456);
$size=count($myArray);

foreach($myArray as $el){
    if(strlen($el) == 3)
    {
    $threeDigits++;
    }
    else if(strlen($el)==4)
    {
        $fourDigits++;
    }
    else if(strlen($el)==5)
    {
    $fiveDigits++;
}
else if (strlen($el)==6)
{
    $sixDigits++;
}
}
echo "there are " .$threeDigits. " three digits numbers -- " ;
echo "there are " .$fourDigits. " four digits numbers -- " ;
echo "there are " .$fiveDigits. " five digits numbers -- " ;
echo "there are " .$sixDigits. " six digits numbers -- " ;
?>

Answered By – Leo

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published