As part of studying algorithms, I needed to make a function to count occurrences of characters in a string. My solution is below. I'd like some feedback about how I did - could the implementation be cleaner? Are there any conceptual gaps my code suggests?
<?php
function ra_count_chars($str){
$dict = str_split(strtolower(str_replace(' ', '', $str)));
foreach($dict as $key=>$value){
$dict[$value] = 0;
unset($dict[$key]);
}
for($i = 0; $i < strlen($str); $i++){
$dict[$str[$i]] += 1;
}
return $dict;
}
print_r(ra_count_chars('ccaaat'));