当前位置:网站首页>PHP sort() function

PHP sort() function

2022-06-24 05:10:00 User 1448800

example

An array $cars The elements in are sorted alphabetically in ascending order :

<?php
$cars=array("Volvo","BMW","Toyota");
sort($cars);
?>

Definition and Usage

sort() Function to sort the index array in ascending order .

notes : This function gives a new key name to the unit in the array . The original key name will be deleted .

Return if successful TRUE, Otherwise return to FALSE.

grammar

sort(array,sortingtype);

Parameters

describe

array

It's necessary . Specify the array to sort .

sortingtype

Optional . Specifies how to compare elements of an array / project . Possible value :0 = SORT_REGULAR - Default . Put each item in the regular order (Standard ASCII, Don't change the type )1 = SORT_NUMERIC - Treat each item as a number .2 = SORT_STRING - Treat each item as a string .3 = SORT_LOCALE_STRING - Treat each item as a string , Based on the current locale ( It can be done by setlocale() Make changes ).4 = SORT_NATURAL - Treat each item as a string , Use similar natsort() Natural ordering .5 = SORT_FLAG_CASE - Can combine ( Press bit or )SORT_STRING or SORT_NATURAL Sort strings , Case insensitive .

  • 0 = SORT_REGULAR - Default . Put each item in the regular order (Standard ASCII, Don't change the type )
  • 1 = SORT_NUMERIC - Treat each item as a number .
  • 2 = SORT_STRING - Treat each item as a string .
  • 3 = SORT_LOCALE_STRING - Treat each item as a string , Based on the current locale ( It can be done by setlocale() Make changes ).
  • 4 = SORT_NATURAL - Treat each item as a string , Use similar natsort() Natural ordering .
  • 5 = SORT_FLAG_CASE - Can combine ( Press bit or )SORT_STRING or SORT_NATURAL Sort strings , Case insensitive .

Technical details

Return value :

Return if successful TRUE, If it fails, return to FALSE.

PHP edition :

4+

More instances

Example 1

An array $numbers The elements in are sorted in ascending numerical order :

<?php
$numbers=array(4,6,2,22,11);
sort($numbers);
?>
原网站

版权声明
本文为[User 1448800]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/08/20210822001432237i.html