PHP Programing language

adplus-dvertising
array_ksort() and array_krsort() Function in PHP
Previous Home Next
adplus-dvertising

array_ksort()

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

Syntax:
krsort(array,sortingtype);

In above syntax, "array" specifies the array to sort and "sortingtype" specifies how to compare the array elements/items by using values are as follows:

  • 0 = SORT_REGULAR - Default. Compare items normally (don't change types)
  • 1 = SORT_NUMERIC - Compare items numerically
  • 2 = SORT_STRING - Compare items as strings
  • 3 = SORT_LOCALE_STRING - Compare items as strings, based on current locale
  • 4 = SORT_NATURAL - Compare items as strings using natural ordering
  • 5 = SORT_FLAG_CASE -

Example:

<?php
$salary=array("John"=>"25000","Roy"=>"17000","Smith"=>"23000");
ksort($salary);
foreach($salary as $x=>$x_value)
    {
    echo "Emp_Name=" . $x . ", Salary=" . $x_value;
    echo "<br>";
    }
?>

Output:

Emp_Name=John, Salary=25000
Emp_Name=Roy, Salary=17000
Emp_Name=Smith, Salary=23000

array_krsort()

The pos() function returns the value of the current element in an array.

Syntax:
pos(array)

In above syntax, "array" specifies the array to sort and "sortingtype" specifies how to compare the array elements/items by using values are as follows:

  • 0 = SORT_REGULAR - Default. Compare items normally (don't change types)
  • 1 = SORT_NUMERIC - Compare items numerically
  • 2 = SORT_STRING - Compare items as strings
  • 3 = SORT_LOCALE_STRING - Compare items as strings, based on current locale
  • 4 = SORT_NATURAL - Compare items as strings using natural ordering
  • 5 = SORT_FLAG_CASE -

Example:

<?php
$salary=array("John"=>"25000","Roy"=>"17000","Smith"=>"23000");
krsort($salary);
foreach($salary as $x=>$x_value)
    {
    echo "Emp_Name=" . $x . ", Salary=" . $x_value;
    echo "<br>";
    }
?>

Output:

Emp_Name=Smith, Salary=23000
Emp_Name=Roy, Salary=17000
Emp_Name=John, Salary=25000

Previous Home Next