PHP ksort() Function

In this chapter you will learn:

  1. Definition for PHP ksort() Function
  2. Syntax for PHP ksort() Function
  3. Parameter for PHP ksort() Function
  4. Note for PHP ksort() Function
  5. Example - Pass a second parameter to the sort functions to specify how you want the values sorted
  6. Example - Sort an associative array in ascending order, according to the key

Definition

The ksort() function takes an array and sorts it by its keys while preserving the values.

Syntax

PHP ksort() Function has the following syntax.

bool ksort ( array &arr [, int options] )

Parameter

ParameterIs RequiredDescription
arrayRequired.Specifies the array to sort
sortingtypeOptional.Specifies how to compare the array elements/items.

Possible values for sortingtype:

ValueDescription
0 = SORT_REGULARDefault. Compare items normally without changing types
1 = SORT_NUMERICCompare items numerically
2 = SORT_STRINGCompare items as strings
3 = SORT_LOCALE_STRINGCompare items as strings, based on current locale
4 = SORT_NATURALCompare items as strings using natural ordering
5 = SORT_FLAG_CASEan be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

Note

Use the krsort() function to sort an associative array in descending order, according to the key.

Use the asort() function to sort an associative array in ascending order, according to the value.


<?PHP//from   j  a  v a2 s.c o  m
    $capitalcities['A'] = 'Z';
    $capitalcities['B'] = 'X';
    $capitalcities['C'] = 'Y';
    ksort($capitalcities);
    print_r($capitalcities);
?>

ksort() works by reference, directly changing the array. The return value is either true or false, depending on whether the sorting was successful.

The code above generates the following result.

Example 1

We can pass a second parameter to the sort functions to specify how you want the values sorted, like this:


<?PHP/*  ja  va  2  s .  com*/
    $array["1"] = "1";
    $array["2"] = "2";
    $array["3"] = "3";
    $array["10"] = "10";
    $array["100"] = "100";
    var_dump($array);
    ksort($array, SORT_STRING);
    var_dump($array);
?>

If you want to force a strictly numeric sort, you can pass SORT_NUMERIC as the second parameter.

The code above generates the following result.

Example 2

Sort an associative array in ascending order, according to the key:


<?php
$age=array("PHP"=>"5","Java"=>"7","java2s.com"=>"3");
ksort($age);
?>

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP list() Function
  2. Syntax for PHP list() Function
  3. Parameter for PHP list() Function
  4. Note for PHP list() Function
  5. Example - Assign value in an array to a list of variables
  6. Example - Using the first and third variables
  7. Example - Compare the code with and without list() function
  8. Example - Use list during array loop
Home » PHP Tutorial » PHP Array Functions
PHP array() function
PHP array_change_key_case() function
PHP array_chunk() function
PHP array_combine() function
PHP array_count_values() function
PHP array_diff() function
PHP array_diff_assoc() Function
PHP array_diff_key() function
PHP array_diff_uassoc() function
PHP array_diff_ukey() function
PHP array_fill() function
PHP array_fill_keys() function
PHP array_filter() function
PHP array_flip() function
PHP array_intersect() Function
PHP array_intersect_assoc() Function
PHP array_intersect_key() function
PHP array_intersect_uassoc() Function
PHP array_intersect_ukey() Function
PHP array_key_exists() Function
PHP array_keys() function
PHP array_map() Function
PHP array_merge() function
PHP array_merge_recursive() Function
PHP array_multisort() Function
PHP array_pad() Function
PHP array_pop() function
PHP array_product() Function
PHP array_push() function
PHP array_rand() function
PHP array_reduce() Function
PHP array_replace() Function
PHP array_replace_recursive() Function
PHP array_reverse() Function
PHP array_search() Function
PHP array_shift() Function
PHP array_slice() Function
PHP array_splice() Function
PHP array_sum() Function
PHP array_udiff() Function
PHP array_udiff_assoc() Function
PHP array_udiff_uassoc() Function
PHP array_uintersect() Function
PHP array_uintersect_assoc() Function
PHP array_uintersect_uassoc() Function
PHP array_unique() Function
PHP array_unshift() Function
PHP array_values() Function
PHP array_walk() Function
PHP array_walk_recursive() Function
PHP arsort() Function
PHP asort() Function
PHP compact() Function
PHP count() Function
PHP current() Function
PHP each() Function
PHP end() Function
PHP explode() Function
PHP extract() Function
PHP implode() Function
PHP in_array() Function
PHP key() Function
PHP krsort() Function
PHP ksort() Function
PHP list() Function
PHP natcasesort() Function
PHP natsort() Function
PHP next() Function
PHP pos() Function
PHP prev() Function
PHP range() Function
PHP reset() Function
PHP rsort() Function
PHP shuffle() Function
PHP sizeof() Function
PHP sort() Function
PHP uasort() Function
PHP uksort() Function
PHP usort() Function