Home  >  Article  >  Backend Development  >  Summary of PHP array sorting methods (Collection)

Summary of PHP array sorting methods (Collection)

WBOY
WBOYOriginal
2016-07-25 08:55:511083browse
  1. $a = array(4,"37",3,100,0,-5);
  2. sort($a);
  3. for ($i=0; $i<6; + +$i){
  4. echo $a[$i]." ";
  5. }
  6. echo "
    ";
  7. sort($a,SORT_STRING);
  8. for ($i=0; $i< 6; ++$i){
  9. echo $a[$i]." ";
  10. }
  11. echo "
    ";
  12. ?>
Copy code

Output result: -5 0 3 4 37 100 -5 0 100 3 37 4

2, descending sort: rsort(array, [sort type]) Parameter usage is the same as the sort function.

Associative array sorting: Function: asort(array, [sort type]) Description: Sort in ascending order based on the element values ​​of the associative array. Parameter usage is as shown in the sort function above.

Function: ksort(array, [sort type]) Description: Sort in ascending order based on the keys of the associative array. Parameter usage is as shown in the sort function above.

Example:

  1. $a = array(

  2. "good" => "bad",
  3. "right" => "wrong",
  4. "boy" => "girl");

  5. echo "value sort
    ";

  6. asort($a);
  7. foreach($a as $key => $value){
  8. echo "$key : $value
    ";
  9. }

  10. echo "
    key sort
    ";

  11. ksort($a);
  12. foreach($a as $key => $value){
  13. echo "$key : $value
    ";
  14. }
  15. ?>

Copy code

Output result: value sort good : bad boy : girl right : wrong

key sort boy : girl good : bad right : wrong

3, Sort in descending order: arsort(array, [sort type]) corresponds to asort krsort(array, [sort type]) corresponds to ksort

Function range() to quickly create an array

For example, the range() function can quickly create an array of numbers from 1 to 9:

  1. $numbers=range(1,9);
  2. echo $numbers[1];
  3. ?>
Copy code

Of course, use range(9,1) Then an array of numbers from 9 to 1 is created. At the same time, range() can also create a character array from a to z:

  1. $numbers=range(a,z);
  2. foreach ($numbers as $mychrs)
  3. echo $mychrs." ";
  4. ?>
Copy code

Pay attention to the case when using character arrays. For example, range(A,z) and range(a,Z) are different. The range() function also has a third parameter, which is used to set the step size. For example, the array elements created by range(1,9,3) are: 1, 4, 7. Common PHP array sorting Generally, each element in the array is represented by characters or numbers, so the array elements can be arranged in ascending order. This function is sort().

For example:

  1. $people=array('name','sex','nation','birth');
  2. foreach ($people as $mychrs)
  3. echo $mychrs." ";
  4. sort($people);
  5. echo "
    ---After sorting---
    ";
  6. foreach ($people as $mychrs)
  7. echo $mychrs." ";
  8. ?>
Copy code

The array elements sorted in ascending order are displayed as birth name nation sex. Of course, the sort() function is case-sensitive (letters from largest to smallest are: A...Z... a…z)

The

Sort() function also has a second parameter, which is used to indicate whether the PHP array sorting rule in ascending order is used to compare numbers or strings. For example:

  1. echo "---Sort in ascending numerical order---
    ";
  2. $num2=array('26','3',);
  3. sort( $num2,SORT_NUMERIC);
  4. foreach ($num2 as $mychrs)
  5. echo $mychrs." ";
  6. echo "
    ---Sort in ascending character order---
    ";
  7. $num3=array('26','3');
  8. sort($num3,SORT_STRING);
  9. foreach ($num3 as $mychrs)
  10. echo $mychrs." ";
  11. ?>
Copy code

SORT_NUMERIC and SORT_STRING are used to declare ascending order of numbers or characters. If arranged in ascending order of numbers, it is: 3, 26; but if arranged in ascending order of characters, it is: 26, 3.

In addition to the ascending function in PHP, there is also a descending or reverse sorting function, which is the rsort() function, for example: $num1=range(1,9);rsort($num1); This is actually equivalent to range( 9,1).

>>> For more information, please view the complete list of php array sorting methods



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn