Convert array to customized format using php -
i have code count frequency of alphabet in string. . code :
<?php $str = "iamtheone"; $freq_count = array(); ($i = 0; $i < strlen($str); $i++) { $index = $str[$i]; $freq_count[$index]++; foreach (range('a', 'z') $char) { //echo "<pre>".$key . " " . $char."</pre>"; $index = $char; if (isset($freq_count[$index])) { } else { $freq_count[$index] = "0"; } } } echo "<pre>"; print_r($freq_count); echo "</pre>"; ?>
the output is:
array ( [i] => 1 [a] => 1 [b] => 0 [c] => 0 [d] => 0 [e] => 2 [f] => 0 [g] => 0 [h] => 1 [j] => 0 [k] => 0 [l] => 0 [m] => 1 [n] => 1 [o] => 1 [p] => 0 [q] => 0 [r] => 0 [s] => 0 [t] => 1 [u] => 0 [v] => 0 [w] => 0 [x] => 0 [y] => 0 [z] => 0 )
now want them convert array in following format:
* * * * * * * * * b c d e f g h j k l m n o p q r s t u v w x y z
explanation: number of asterisks depends on how many frequency of each alphabet. example, a
1 time repeat in string , e
2 times repeated in string , on.
is array format correct? suggestion? thank you
your teacher won't believe did without help, but:
$str ="iamtheone"; // calculate frequency table letters $letters = array_fill_keys(range('a', 'z'), ' '); $freq_count = array_merge( $letters, array_count_values(str_split(strtolower($str))) ); // plot display chart ($line = max($freq_count); $line > 0; --$line) { echo implode( ' ', array_map( function ($value) use ($line) { return ($value >= $line) ? '*' : ' '; }, $freq_count ) ); echo php_eol; } echo implode(' ', array_keys($linearray)), php_eol;
Comments
Post a Comment