Php algorithm for counting the number of times all characters appear in a string: loop a string ($ str in this example), record the existing strings in an array (such as $ strRecord in this example) if the record function already exists, it is not recorded. when each string is used, it is used with the & #20540; compare (in this example, $ st php counts the number of times all characters appear in the string.
Effect
Algorithm:
Loop a string ($ str in this example) and record the existing string in an array (such as $ strRecord in this example). If the record function already exists, no record;
Compare each string with the value of the record array (in this example, $ strRecord [] ['key']). If a value in the record is the same as this string, the record count + 1 ($ strRecord [] ['count'] in this example);
Of course, to set a variable, the default value is false (such as $ found in this example). The record is completely Compared. if the record array already has this value, it is set to true, record an array that has not been encountered to an array
Code:
<? Php // count the characters in the string, occurrences echo '<pre>'; $ str = 'aaabbccqqwweedfghhjffffffffggggggggg '; // string example echo $ str. '<br/>'; $ strRecord = array (); // record existing characters in this array. if any, no record is recorded, for ($ I = 0; $ I <strlen ($ str); $ I ++) {$ found = 0; // It is set to no foreach (array) $ strRecord as $ k =>$ v) {if ($ str [$ I] ==$ v ['key']) {$ strRecord [$ k] ['count'] + = 1; // already encountered, count + 1; $ found = 1; // you have already set something you have already encountered, mark continue; // if you have already encountered this problem, you do not need to recycle the record array and continue the next string comparison} if (! $ Found) {$ strRecord [] = array ('key' => $ str [$ I], 'count' => 1 ); // record a string that has not been encountered} print_r ($ strRecord);?>