0

I had to make an array with as indexes A-Z (the alphabet). Each index had to have a value 0. So i made this array:

$alfabet = array(
'A' => 0,
'B' => 0,
'C' => 0,
'D' => 0,
'E' => 0,
'F' => 0,
'G' => 0,
'H' => 0,
'I' => 0,
'J' => 0,
'K' => 0,
'L' => 0,
'M' => 0,
'N' => 0,
'O' => 0,
'P' => 0,
'Q' => 0,
'R' => 0,
'S' => 0,
'T' => 0,
'U' => 0,
'V' => 0,
'W' => 0,
'X' => 0,
'Y' => 0,
'Z' => 0
);

I also have got text from a file ($text = file_get_contents('tekst15.txt');) I have putted the chars in that file to an array: $textChars = str_split ($text); and sorted it from A-Z: sort($textChars);

What i want is that (with a for loop) when he finds an A in the textChars array, the value of the other array with index A, goes up by one (so like: $alfabet[A]++;

Can anyone help me with this loop? I have this atm:

for($i = 0; $i <= count($textChars); $i++){

while($textChars[$i] == $alfabet[A]){
$alfabet[A]++;
}


}
echo $alfabet[A];

Problem 1: i want to loop the alfabet array to, so now i only check for A but i want to check all indexes. Problem2: this now returns 7 for each alphabet index i try so its totally wrong :)

I'm sorry about my english but thanks for your time.

2
  • Why not use a foreach loop? Commented Nov 25, 2012 at 16:50
  • 1
    You could use count_chars() instead. Commented Nov 25, 2012 at 17:05

4 Answers 4

2

Heard of the foreach loop?

foreach ($textChars as $index => $value) {
    $alfabet[$value]++;
}
Sign up to request clarification or add additional context in comments.

3 Comments

are the $index and $value also arrays? I'm confused
@AxelLambregts: $index and $value will be populated with the current iteration's element index and element's value. Please read the added link.
-1. This code doesn't answer the question, it just increases each entry of the frequency table.
0

I assume that your $textChars array looks like

$textChars = array (
    0 => 'A',
    1 => 'A',
    2 => 'B',
);

If so you can loop through it and use it's values to check if given index exists in $alfabet and then increment it.

foreach($textChars as $char){
    if(isset($alfabet[$char])){
        $alfabet[$char]++;
    }
}

Comments

0
$fp = fopen('tekst15.txt', 'r');
if (!$fp) {
    echo 'Could not open file tekst15.txt';
}
while (false !== ($char = fgetc($fp))) {
    if(isset($alfabet[strtoupper($char)]))
    { $alfabet[strtoupper($char)] = $alfabet[strtoupper($char)]+1; }
}

Comments

0

The count_chars() function can give you that information immediately:

$stats = count_chars(file_get_contents('tekst15.txt'));

echo $stats['A']; // number of 'A' occurrences
echo $stats['O']; // number of 'O' occurrences

From your code:

while($textChars[$i] == $alfabet[A]){
   $alfabet[A]++;
}

Made no sense at all; it compares each character from the text file to the value of $alfabet[A] which is 0 at first (not even a letter!).

The correct statement would be:

$alfabet[$textChars[$i]]++;

Comments

Your Answer

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