PHP: implementation of the array operation function array_count_values ()

Source: Internet
Author: User
PHP: implementation of the array operation function array_count_values () As a weak scripting language, PHP does not need to declare its variables. it is ready to use. its array is very different from other strong languages, such as the association key and value key in the PHP array, the most interesting one is the association key, which cannot be seen in C ++ or JAVA, and PHP also provides many powerful array operation functions, such

Array_values () // you can strip the join key and numeric key in the array, or obtain an array consisting of the values of its elements.

Array_keys () // obtain all the associated keys and numeric keys.

Using these two functions can be very convenient and simple implementation.

Array_count_values () function

The idea is to first use array_values () or get an array of element values.

Then, use the element value array as the join key to create an array (check whether the key exists first)

Then, use foreach to loop through the original array and use the element value as the join key of the new array.

The expression may not be very clear.

 0); // connecting two arrays is actually equivalent to adding the element $ count = array_merge ($ count, $ temp);} foreach ($ values as $ key) {$ count [$ key] ++;} return $ count;} return $ arr;} $ a = array ("ABC", "FUCKYOU", "ABC ", "Dady", "PO", "Dady", "LIN", "ABC", "LIN", "FUCKYOU"); $ a = mycount ($ ); print_r ($ a);?>

Running result

Array ([ABC] => 3 [FUCKYOU] => 2 [Dady] => 2 [PO] => 1 [LIN] => 2)

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.