PHP Development Notes Series (ix)-Arrays (iii)

Source: Internet
Author: User
Tags sorts
PHP Development Note Series (ix)-Arrays (iii)

??? Wrote two articles about the daily use of PHP arrays, this "PHP Development Note Series (ix)-Array (iii)", focusing on the size and sorting of arrays.


??? 1. Get the array length

??? The count () function returns the number of elements in the array, which is the alias of sizeof (), which is the same function as the count ().

?

file:count.phpurl:http://localhost:88/array/count.php
 
  

?

??? The count () function can accept a mode parameter in addition to the first array variable that is counted, specifying whether recursion is required, and the number of all elements in the multidimensional array, with the following code:

?

file:count.phpurl:http://localhost:88/array/count.php
 
  

?

??? 2. Statistics of array elements occurrence frequency

??? The Array_count_values () function returns an array containing the associated key/value. If ' A ' appears 2 times, ' B ' appears 1 times, and so on. The code is as follows:

?

file:array_count_values.phpurl:http://localhost:88/array/array_count_values.php
 
   ====================
"; $favor 1 = Array (' 1 ' = ' Sport ', ' 2 ' = ' Sing ', ' 3 ' = ' Sing ', ' 4 ' = ' travel '); $stats 1 = array_count_values ($favor 1); Print_r ($stats 1); ? >

?

??? 3. Returning an array of non-repeating elements

??? The Array_unique () function removes all duplicate values in the array, returns an array of distinct values, the original array element unchanged, and the code as follows:

?

file:array_unique.phpurl:http://localhost:88/array/array_unique.php
 
   ====================
"; Print_r ($unique); ? >

?

??? 4. Array sorting

??? The sort () function sorts the array, and the elements are arranged in the order of the values from lowest to highest. The sort () function does not renege on the sorted array, but instead it simply sorts, regardless of the result of any value. At the same time, you can specify a collation for the sort, such as sort_numeric is sorted by numeric value, applied on integers and floating-point numbers, Sort_regular is sorted by ASCII value, applied on characters or strings, and sort_string is sorted by string. The code is as follows:

??

??? Note: the sort () function modifies the values in the incoming array directly, looking closely at the output of the program below.

?

file:sort.phpurl:http://localhost:88/array/sort.php
 
   ';    Sort (, sort_numeric);    Print_r ();        echo "
====================
"; $letter = Array (' A ', ' a ', ' Z ', ' f ', ' G ', ' e '); Print_r ($letter); Echo '
'; The default is that the pattern is sort_regular sort ($letter, sort_regular); Print_r ($letter); echo "
====================
"; $string = Array (' Jack ', ' Mike ', ' Mary ', ' jassica ', ' Ruby '); Print_r ($string); Echo '
'; Sort ($string, sort_string); Print_r ($string); ? >

?

??? From the above results can be viewed, the sort () function modifies not only the elements passed into the array, but also the corresponding relationship between the key and the value changes, if you need to maintain the relationship between the key/value pairs, you need to use the Asort () function, as follows:

?

file:asort.phpurl:http://localhost:88/array/asort.php
 
   ';    Asort (, sort_numeric);    Print_r ();        echo "
====================
"; $letter = Array (' A ', ' a ', ' Z ', ' f ', ' G ', ' e '); Print_r ($letter); Echo '
'; The default is that the pattern is sort_regular asort ($letter, sort_regular); Print_r ($letter); echo "
====================
"; $string = Array (' Jack ', ' Mike ', ' Mary ', ' jassica ', ' Ruby '); Print_r ($string); Echo '
'; Asort ($string, sort_string); Print_r ($string); ? >
?

??? 5. Reverse Order

??? The Rsort () function is similar to the sort function, and has the same pattern of preserving key-value pairs and not maintaining a key-value-pair relationship.


??? 6. Natural sorting of arrays

??? In the sort of array, such as Student1.jpg, Student2.jpg,student10.jpg, STUDENT20.JGP, the use of a typical sort, such as sort, will drain student1.jpg, studdent10.jpg, Student2.jpg, student20.jpg such order, but the order of expectation is student1.jpg, student2.jpg, Studdent10.jpg, student20.jpg. You need to use the Natsort () function.

?

file:natsort.phpurl:http://localhost:88/array/natsort.php
 
  
"; Sort ($student); Print_r ($student); echo "

"; Natsort ($student); Print_r ($student); ? >
?

??? The Natsort () function also has a variant called the Natcasesort () function, which is functionally the same as the Natsort () function, except that it is not short of case-sensitive.

?

file:natcasesort.phpurl:http://localhost:88/array/natcasesort.php
 
  

?

??? 7. Sorting the key values in an array

??? The above function mainly sorts the numeric key array, then sorts the associative key array, may have the special demand, for example sorts according to the key value, needs to use the Ksort () function, returns True successfully, the failure returns false. Also, like the sort () function, accepts a sort_flags parameter and specifies the sort pattern for sort_numeric,sort_regular,sort_string. The code is as follows:

?

file:ksort.phpurl:http://localhost:88/array/ksort.php
 
  
"; Ksort ($student); Print_r ($student); ? >

?

??? 8. Custom Collation

??? PHP also provides a custom sort method, which is Usort (), which can be ordered using the user-defined comparison algorithm specified in the function. But generally used less, we have to experiment.


???


??? The ordering section of the PHP array is the next continuation of the PHP array.


??? This article address:http://ryan-d.iteye.com/blog/1566686

?

?

  • 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.