Count the number of vowels in an array in PHP

In PHP, I wrote a script to count the number of vowels in an array and output the result.

I get an error when running the script Undefined offset

.

What's wrong with this code?

for($a = 0; $a < count($expld); $a++) {
    for($b = 0; $b < strlen($expld[$b]); $b++) {
        if ($expld[$b] == 'A' || $expld[$b] == 'a' || $expld[$b] == 'E' || $expld[$b] == 'e'
            || $expld[$b] == 'I' || $expld[$b] == 'i' || $expld[$b] == 'O' || $expld[$b] == 'o'
            || $expld[$b] == 'U' || $expld[$b] == 'u')
        {
            $vowel++;
        }
    }
    echo "$expld[$a] has $vowel vowels(s).<br> ";
}

      

+3


source to share


4 answers


You seem to be pretty confused about indexing, check it out:



for($a = 0; $a < count($expld); $a++)
{
    $vowel = 0;
    for($b = 0; $b < strlen($expld[$a]); $b++)
    {
        if($expld[$a][$b] == 'A' || $expld[$a][$b] == 'a' || $expld[$a][$b] == 'E' || $expld[$a][$b] == 'e'
        || $expld[$a][$b] == 'I' || $expld[$a][$b] == 'i' || $expld[$a][$b] == 'O' || $expld[$a][$b] == 'o'
        || $expld[$a][$b] == 'U' || $expld[$a][$b] == 'u')
        {
          $vowel++;
        }
    }
    echo "$expld[$a] has $vowel vowels(s).<br> ";
}

      

+1


source


As another possible solution. You can use regex:

$stringToTest = implode($expld);
$vowelsCount = strlen(preg_replace('/[^aeiouAEIOU]/', '', $stringToTest));

      



Here we have removed everything except the vowels from the string and then counted the rest of the characters (which should only be vowels).

+2


source


You can check if a character exists in the vowel array. This way, you don't need to check for a long time! Here is the code for that ...

// vowels
$vowels = array('a', 'e', 'i', 'o', 'u');

for ($a = 0; $a < count($expld); $a++) {
    // init count
    $vowel_count = 0;

    for ($b = 0; $b < strlen($expld[$b]); $b++) {
        if (in_array(strtolower($expld[$a][$b]), $vowels)) {
            $vowel_count++;
        }
    }
    echo "$expld[$a] has $vowel_count vowels(s).";
}

      

Hope this is helpful. Thank!

+2


source


First, a few notes:

  • You seem to be using the $ a and $ b indexes incorrectly; your second for-loop is ignoring where it is in $ a! Since you are iterating over each line in the array, a safer bet might be to use a foreach-loop instead (another thing to keep track of)
  • You are not defining your vowel count, so it will not show 0. Also, since you are not resetting the vowel counter, the count will keep increasing.

Here's an example that uses a foreach loop (tested in PHP 5.4+):

$input = "Charles Schultz was here";

// Instead of adding a large if-elseif-elseif-... statement, define
//   the vowels in an array, then check if the value is in that array
$vowels = array('a','e','i','o','u','A','E','I','O','U');
$exploded = explode(" ", $input);

foreach($exploded as $string) {
    // Reset the counter for each word
    $count_vowels = 0;
    for($i = 0; $i < strlen($string); $i++) {
        if(in_array($string[$i], $vowels)) {
            $count_vowels++;
        }
    }
    echo "$string has $count_vowels vowel(s).<br />";
}

      

+1


source







All Articles