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 ()