Function for counting the number and uniqueness of array elements

Source: Internet
Author: User

Some functions can be used to determine the total number of values in the array and the number of unique values. Use the count () function to calculate the number of elements. The alias of Count () in sizeof () function has the same function.

① Function count ()

The count () function is used to calculate the number of elements in an array or the number of attributes in an object. For an array, return the number of its elements. For other values, return 1. If the parameter is a variable and the variable is not defined or the variable contains an empty array, the function returns 0. The function has two parameters. The function prototype is as follows:

Int count (mixed var [, int mode])

The first parameter is required to pass in the array or object to be counted. The second parameter is optional, specifying whether the function mode recursively calculates the number of elements in the array of multi-dimensional arrays. Values 0 and 1 are the default values. If the value is 1, the multi-dimensional array is not detected. The code used by the count () function is as follows:

12345678910111213 <?php    $lamp = array("Linux","Apache","MySQL","PHP");    echo count($lamp);          // Number of output arrays: 4     // Declare a two-dimensional array to count the number of elements in the array    $web = array(        ‘lanmp‘ =>array("Linux","Apache","MySQL","PHP"),        ‘j2ee‘ =>array("Unix","Tomcat","Oracle","JSP")    );     echo count($web,1);             // If the second parameter is set to 1, calculate the number of multi-dimensional arrays and output 10    echo count($web)                // The default mode is 0. If the number of arrays is not calculated, 2 is output.?>

② Function array_count_values ()

The array_count_values () function is used to count the number of occurrences of all values in the array. This function has only one parameter. Its prototype is as follows:

Array array_count_values (array input)

The parameter specifies that an array is input and an array is returned. The key name of the element is the value of the original array, and the key value is the number of times that the value appears in the element group. The code used by the array_count_values () function is as follows:

1234567 <?php    $array =array(1,‘php‘,1,‘mysql‘,‘php‘);     // Min and an array with repeated values     $newArray = array_count_values($array);     // Counts the number of times all values in the array $ array appear     print_r($newArray);     // Output: array ([1] => 2 [PHP] => 2 [MySQL] => 1)?>

③ Function array_unique ()

The array_unique () function is used to delete repeated values in an array and return a new array with no repeated values. The function is prototype as follows:

Array array_unique (array)

The parameter needs to receive an array. When the values of several elements in the array are equal, only one element is retained, and other elements are deleted. Array_unique () First sorts the values as strings, then retains only the first key name that is encountered for each value, and then ignores all the key names that follow. This does not mean that the first key name that appears for the same value in the unordered array will be retained. The code used by the array_unique () function is as follows:

1234 <?php    $a = array("a"=>"php","b"=>"mysql","c"=>"php");            // Declare an array with repeated values    print_r(array_unique($a));          // Output after deleting the duplicate value: array ([a] => PHP [B] => MySQL)?>
 

> Fixed link: http://php.ncong.com/php_course/arry_function/array_tongji.html

> For Reprinted Information, please note: ncong PHP was published in ncong PHP learning tutorial on September 10, April 13, 2014.

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.