Home  >  Article  >  Backend Development  >  PHP array sorting ksort-sort the element keys of the array in ascending order_PHP tutorial

PHP array sorting ksort-sort the element keys of the array in ascending order_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:12:501423browse

PHP array sort sorts the element keys of the array in ascending order and maintains the index relationship. Friends in need can refer to it.

This time the function is
The ksort() function sorts the array in ascending order by key name, retaining the original keys for the array values.

The optional second parameter contains additional sorting flags.

Returns TRUE if successful, otherwise returns FALSE.

Grammar
ksort(array,sorttype) parameter description
array required. Specifies the array to be sorted.
sorttype optional. Specifies how to arrange the values ​​of an array. Possible values:

SORT_REGULAR - Default. Processed with their original type (without changing type).
SORT_NUMERIC - treat values ​​as numbers
SORT_STRING - handle values ​​as strings
SORT_LOCALE_STRING - Handle values ​​as strings, based on local settings*.

*: This value is newly added in PHP 4.4.0 and 5.0.2. Prior to PHP 6, the system locale was used, which could be changed with setlocale(). Since PHP 6, the i18n_loc_set_default() function must be used.

Example

The code is as follows
 代码如下 复制代码

$my_array = array("e" => "Dog", "b" => "Cat", "c" => "Horse");
ksort($my_array);
print_r($my_array);
?>输出:

Array(    [b] => Cat    [c] => Horse    [e] => Sog)

Copy code
$my_array = array("e" => "Dog", "b" => "Cat", "c" => "Horse");

ksort($my_array);

print_r($my_array); Array( [b] => Cat [c] => Horse [e] => Sog)
http://www.bkjia.com/PHPjc/629256.html
www.bkjia.com
truehttp: //www.bkjia.com/PHPjc/629256.htmlTechArticlephp array sort sorts the element key names of the array in ascending order and maintains the index relationship. Friends in need can refer to it . This time the function is ksort(). The function sorts the array according to the key name...
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