Record the solution of a problem, which involves the use of several functions, as a review.
Explain the problem first. The field content in the datasheet stores a comma-delimited string with a maximum number of 20 and a maximum of 40. For example, 3,24,33,40 a sequence of numbers similar to the typeface. It's actually a field that holds multiple voting results. Now we need to count the number of each number, that is, how many people voted for each vote, and sort.
My train of thought is this.
1. First read the data from the Congtent field of the database and merge them into a single string.
Fetch_array ($result)) {//$r [] = Explode (",", $myrow ["content"]), $str. = $myrow ["Content"]. ', ';} $arr _str = substr ($str, 0,-1);? >
Because the last number is followed by a comma, the string is intercepted.
2. Divide the string into groups by commas.
$r = Explode (",", $arr _str);
3. Using Array_count_values () to count the number of elements in a one-dimensional array
Since Array_count_values () does not seem to be able to directly count the elements of a two-dimensional array, the above two steps are performed to get a one-dimensional array.
The Array_count_values () function is used to count the occurrences of all values in an array. Returns an array whose element's key name is the value of the original array, and the key value is the number of occurrences of the value in the original array.
$rs = Array_count_values ($r);
4. Sorting
Sorting is simple, but to keep the key value, you can use your own asort () function.
Asort ($RS); Echo '';p rint_r ($rs); Echo '
';
Record a few functions that are used.
http://www.bkjia.com/PHPjc/752358.html www.bkjia.com true http://www.bkjia.com/PHPjc/752358.html techarticle record the solution of a problem, which involves the use of several functions, as a review. Explain the problem first. The field content in the data table stores a comma-separated word ...