How to loop an array with strings as indexes in PHP
Posted
by
Axel Lambregts
on Stack Overflow
See other posts from Stack Overflow
or by Axel Lambregts
Published on 2012-11-25T16:49:15Z
Indexed on
2012/11/25
17:04 UTC
Read the original article
Hit count: 246
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.
© Stack Overflow or respective owner