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)