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.

share|improve this question
Why not use a foreach loop? – peterbond Nov 25 '12 at 16:50
1  
You could use count_chars() instead. – Jack Nov 25 '12 at 17:05

4 Answers

up vote 3 down vote accepted

Heard of the foreach loop?

foreach ($textChars as $index => $value) {
    $alfabet[$value]++;
}
share|improve this answer
are the $index and $value also arrays? I'm confused – Axel Lambregts Nov 25 '12 at 17:01
@AxelLambregts: $index and $value will be populated with the current iteration's element index and element's value. Please read the added link. – Madara Uchiha Nov 25 '12 at 17:02
Thank you for your help! – Axel Lambregts Nov 25 '12 at 17:15
-1. This code doesn't answer the question, it just increases each entry of the frequency table. – Jack Nov 25 '12 at 17:22
@Jack: True, I've misread his code. I've edited. – Madara Uchiha Nov 25 '12 at 17:26

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]++;
    }
}
share|improve this answer
Thank you, the 'isset' is wonderfull! – Axel Lambregts Nov 25 '12 at 17:14

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]]++;
share|improve this answer
$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; }
}
share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.