Counting the number of elements in a two-dimensional array _php tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.