PHP: Implementation of array manipulation function array_count_values ()

Source: Internet
Author: User

PHP as a weak type of scripting language, its variables do not need to be declared, that is, the array is much different from other strongly typed languages, such as the PHP array of association keys and numeric keys, the most interesting is the association key, which is not seen in C + + or Java, And PHP also provides many powerful array manipulation functions, such as

array_values ()//Can peel an array of associative and numeric keys in an array, or have the values of its elements

Array_keys () //Get all the correlation keys and numeric keys

Using these two functions can be very convenient and simple to implement

Array_count_values () function

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

Create an array of element values as the association key (you must first check if the association key exists)

Then use the Foreach Loop as the original array to associate the element value with the new array's key operation.

May express not very clearly, directly on the code 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0007.gif "alt=" J_0007.gif "/>

<?php//frequency function Mycount ($arr) {if (Is_array ($arr)) {$values = Array_values ($arr) of the data in the statistics array, and//Gets the value in the array $count = Array ();//Initialize arrays foreach ($values as $value) {if (!array_key_exists ($value, $count)) {//If the key does not exist then create an association key $temp = Array ($value = >0);//connecting two arrays is actually equivalent to adding elements inside $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 ($a);p rint_r ($a);? >

Run results

Array ([ABC] = 3 [Fuckyou] = 2 [Dady] = 2 [PO] + 1 [LIN] + 2)

This article is from the "Debug Me" blog, so be sure to keep this source http://zaitebie.blog.51cto.com/11179646/1741591

PHP: Implementation of array manipulation function array_count_values ()

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.